From ac2b9ba9ce3e1f6d1cc7868fb5a89b43da188b2f Mon Sep 17 00:00:00 2001 From: Akinimaginable Date: Wed, 4 Oct 2023 01:49:14 +0200 Subject: [PATCH 1/2] :zap: improved some return statements and variables allocations --- .env | 0 main.go | 7 ++++--- utils.go | 14 +++++--------- 3 files changed, 9 insertions(+), 12 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go index fb83c5e..d169ef9 100644 --- a/main.go +++ b/main.go @@ -15,13 +15,15 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { case "GET": if path == "/" { http.ServeFile(w, r, "./static/index.html") - } else if strings.HasPrefix(path, "/static/") { fs := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) } else { if urlExist(path) { - io.WriteString(w, "exist") + _, err := io.WriteString(w, "This plak exists") + if err != nil { + return + } } else { w.WriteHeader(http.StatusNotFound) } @@ -37,7 +39,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusMethodNotAllowed) } } - } func main() { diff --git a/utils.go b/utils.go index 6869919..7f9cef9 100644 --- a/utils.go +++ b/utils.go @@ -3,13 +3,13 @@ package main import ( "crypto/rand" "encoding/hex" + "log" mathrand "math/rand" ) func generateUrl() string { - length := currentConfig.urlLength listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - b := make([]rune, length) + b := make([]rune, currentConfig.urlLength) for i := range b { b[i] = listChars[mathrand.Intn(len(listChars))] } @@ -21,17 +21,13 @@ func generateSecret() string { key := make([]byte, 32) _, err := rand.Read(key) if err != nil { - // handle error here + log.Printf("Failed to generate secret") } - secret := hex.EncodeToString(key) - return secret + return hex.EncodeToString(key) } func urlExist(url string) bool { exist := connectDB().Exists(ctx, url).Val() - if exist == 1 { - return true - } - return false + return exist == 1 } From 30185478769bcb7e10ff97a4249d051e14c20074 Mon Sep 17 00:00:00 2001 From: Akinimaginable Date: Thu, 5 Oct 2023 23:18:02 +0200 Subject: [PATCH 2/2] :goal_net: handled unhandled errors + moved default config from code to .env file --- .env | 7 +++++++ config.go | 42 +++++++++++++++++++++++------------------- go.mod | 6 +++++- main.go | 11 +++++++++-- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/.env b/.env index e69de29..0e99fe4 100644 --- a/.env +++ b/.env @@ -0,0 +1,7 @@ +PLAKKEN_INTERFACE=0.0.0.0 +PLAKKEN_PORT=3000 +PLAKKEN_REDIS_ADDR=localhost:6379 +PLAKKEN_REDIS_USER= +PLAKKEN_REDIS_PASSWORD= +PLAKKEN_REDIS_DB=0 +PLAKKEN_REDIS_URL_LEN=5 diff --git a/config.go b/config.go index 3126aeb..d9c0ab4 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,8 @@ package main import ( + "github.com/joho/godotenv" + "log" "os" "strconv" ) @@ -15,38 +17,40 @@ type config struct { urlLength int } -func setConfig() config { - host := os.Getenv("PLAKKEN_HOST") +func getConfig() config { + err := godotenv.Load() + if err != nil { + log.Fatalf("Error loading .env file: %v", err) + } port := os.Getenv("PLAKKEN_PORT") - if port == "" { - port = "3000" - } redisAddr := os.Getenv("PLAKKEN_REDIS_ADDR") - if redisAddr == "" { - redisAddr = "localhost:6379" + db := os.Getenv("PLAKKEN_REDIS_DB") + uLen := os.Getenv("PLAKKEN_REDIS_URL_LEN") + + if port == "" || redisAddr == "" { + log.Fatal("Missing or invalid PLAKKEN_PORT or PLAKKEN_REDIS_ADDR") } - redisUser := os.Getenv("PLAKKEN_REDIS_USER") - redisPassword := os.Getenv("PLAKKEN_REDIS_PASSWORD") - redisDB, err := strconv.Atoi(os.Getenv("PLAKKEN_REDIS_DB")) + redisDB, err := strconv.Atoi(db) if err != nil { - redisDB = 0 + log.Fatal("Invalid PLAKKEN_REDIS_DB") } - urlLength, err := strconv.Atoi("PLAKKEN_URL_LENGTH") + urlLen, err := strconv.Atoi(uLen) if err != nil { - urlLength = 3 + log.Fatal("Invalid PLAKKEN_REDIS_URL_LEN") } - s := config{ - host: host, + + conf := config{ + host: os.Getenv("PLAKKEN_INTERFACE"), port: port, redisAddr: redisAddr, - redisUser: redisUser, - redisPassword: redisPassword, + redisUser: os.Getenv("PLAKKEN_REDIS_USER"), + redisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"), redisDB: redisDB, - urlLength: urlLength, + urlLength: urlLen, } - return s + return conf } diff --git a/go.mod b/go.mod index 4996e79..8331b11 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,12 @@ module plakken go 1.21 +require ( + github.com/joho/godotenv v1.5.1 + github.com/redis/go-redis/v9 v9.2.1 +) + require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/redis/go-redis/v9 v9.2.1 // indirect ) diff --git a/main.go b/main.go index d169ef9..f3370c7 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io" + "log" "net/http" "strings" ) @@ -42,12 +43,18 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { } func main() { - currentConfig = setConfig() + currentConfig = getConfig() listen := currentConfig.host + ":" + currentConfig.port http.HandleFunc("/", handleRequest) + if currentConfig.host == "" { + fmt.Println("Listening on port " + listen) + } else { + fmt.Println("Listening on " + listen) + } + err := http.ListenAndServe(listen, nil) if err != nil { - fmt.Println(err) + log.Fatal(err) } }