minor logic refactor

This commit is contained in:
Tom Andrade 2019-09-19 12:43:05 +02:00
parent 26263cedc6
commit 583d6abb84
Signed by: wolvie
GPG Key ID: 31AAB07872E82669

30
main.go
View File

@ -22,7 +22,7 @@ import (
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
) )
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Base strings for RandStringBytesMaskImprSrc const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Base strings for randStringBytesMaskImprSrc
const ( const (
letterIdxBits = 6 // 6 bits to represent a letter index letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
@ -69,7 +69,11 @@ func redirect(w http.ResponseWriter, r *http.Request, path string) {
rgx, _ := regexp.Compile("[a-zA-Z0-9]+") rgx, _ := regexp.Compile("[a-zA-Z0-9]+")
key = rgx.FindString(key) key = rgx.FindString(key)
key, status := get(key) key, status := get(key)
if status { if !status {
w.WriteHeader(http.StatusNotFound)
notFoundTmpl.Execute(w, nil)
return
}
u, _ := url.Parse(key) u, _ := url.Parse(key)
if u.Scheme == "" { if u.Scheme == "" {
u.Scheme = "https" u.Scheme = "https"
@ -87,30 +91,28 @@ func redirect(w http.ResponseWriter, r *http.Request, path string) {
// then if writes the kv pair suffix, url to the database and return the // then if writes the kv pair suffix, url to the database and return the
// shortened url to the user // shortened url to the user
func shortner(w http.ResponseWriter, r *http.Request, proto, domain, hostSuf, path string, urlSize int) { func shortner(w http.ResponseWriter, r *http.Request, proto, domain, hostSuf, path string, urlSize int) {
if govalidator.IsURL(r.FormValue("url")) { if !govalidator.IsURL(r.FormValue("url")) {
w.WriteHeader(http.StatusBadRequest)
badRequestTmpl.Execute(w, nil)
return
}
u, _ := url.Parse(r.FormValue("url")) u, _ := url.Parse(r.FormValue("url"))
suffix := randStringBytesMaskImprSrc(urlSize)
suffix := RandStringBytesMaskImprSrc(urlSize)
for { for {
_, status := get(suffix) _, status := get(suffix)
if status { if !status {
suffix = RandStringBytesMaskImprSrc(urlSize)
} else {
break break
} }
suffix = randStringBytesMaskImprSrc(urlSize)
} }
set(u.String(), suffix) set(u.String(), suffix)
shortend := proto + "://" + domain + hostSuf + path + suffix shortend := proto + "://" + domain + hostSuf + path + suffix
returnTmpl.Execute(w, shortend) returnTmpl.Execute(w, shortend)
} else {
w.WriteHeader(http.StatusBadRequest)
badRequestTmpl.Execute(w, badRequestTmpl)
}
} }
// RandStringBytesMaskImprSrc Generate random string of n size // randStringBytesMaskImprSrc Generate random string of n size
func RandStringBytesMaskImprSrc(n int) string { func randStringBytesMaskImprSrc(n int) string {
b := make([]byte, n) b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {