From 1f98250626cd0687a8511b9f27a4024ba9f55b7e Mon Sep 17 00:00:00 2001 From: hacki Date: Wed, 4 Oct 2023 01:49:14 +0200 Subject: [PATCH] :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 }