improved some return statements and variables allocations

This commit is contained in:
Hacki 2023-10-04 01:49:14 +02:00
parent c26d6b7e9c
commit ac2b9ba9ce
3 changed files with 9 additions and 12 deletions

0
.env Normal file
View file

View file

@ -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() {

View file

@ -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
}