⚡ improved some return statements and variables allocations
This commit is contained in:
parent
7d45a1792b
commit
1f98250626
3 changed files with 9 additions and 12 deletions
0
.env
Normal file
0
.env
Normal file
7
main.go
7
main.go
|
@ -15,13 +15,15 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
case "GET":
|
case "GET":
|
||||||
if path == "/" {
|
if path == "/" {
|
||||||
http.ServeFile(w, r, "./static/index.html")
|
http.ServeFile(w, r, "./static/index.html")
|
||||||
|
|
||||||
} else if strings.HasPrefix(path, "/static/") {
|
} else if strings.HasPrefix(path, "/static/") {
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
fs := http.FileServer(http.Dir("./static"))
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||||
} else {
|
} else {
|
||||||
if urlExist(path) {
|
if urlExist(path) {
|
||||||
io.WriteString(w, "exist")
|
_, err := io.WriteString(w, "This plak exists")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
@ -37,7 +39,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
14
utils.go
14
utils.go
|
@ -3,13 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"log"
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
func generateUrl() string {
|
func generateUrl() string {
|
||||||
length := currentConfig.urlLength
|
|
||||||
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||||
b := make([]rune, length)
|
b := make([]rune, currentConfig.urlLength)
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = listChars[mathrand.Intn(len(listChars))]
|
b[i] = listChars[mathrand.Intn(len(listChars))]
|
||||||
}
|
}
|
||||||
|
@ -21,17 +21,13 @@ func generateSecret() string {
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
_, err := rand.Read(key)
|
_, err := rand.Read(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error here
|
log.Printf("Failed to generate secret")
|
||||||
}
|
}
|
||||||
|
|
||||||
secret := hex.EncodeToString(key)
|
return hex.EncodeToString(key)
|
||||||
return secret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlExist(url string) bool {
|
func urlExist(url string) bool {
|
||||||
exist := connectDB().Exists(ctx, url).Val()
|
exist := connectDB().Exists(ctx, url).Val()
|
||||||
if exist == 1 {
|
return exist == 1
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue