git.sophuwu.com > urlshort
made function to put urls into db and load the page with short url
added http error check function
load favicon
sophuwu sophie@sophuwu.site
Fri, 01 Sep 2023 01:40:16 +0100
commit

e1c182f0a18d93d73a3613ad5e607f0c74a023bf

parent

7510a5fbed20f0b7dcf6f75ac7bf8b7a091ce59a

1 files changed, 115 insertions(+), 33 deletions(-)

jump to
M main.gomain.go

@@ -15,12 +15,10 @@ var urlDB *bolt.DB

var wordList []string type urlStruc struct { - isCustom bool - creator string - date time.Time - path string - url string - uses int + date time.Time + path string + url string + uses int } func fec(err error) { // fatal error check

@@ -50,11 +48,8 @@ }))

urlDB = db } -func getWords() string { - word := func() string { - return wordList[rand.Intn(len(wordList))] - } - return word() + "-" + word() + "-" + word() +func getWord() string { + return wordList[rand.Intn(len(wordList))] } func readURL(words string) (urlStruc, error) {

@@ -113,6 +108,21 @@ }

w.Write(b) } +func hec(w http.ResponseWriter, err error, code int) bool { + if err == nil { + return false + } + switch code { + case 404: + http404(w) + break + case 500: + http500(w) + break + } + return true +} + func intStr(n int) string { if n == 0 { return "0"

@@ -132,45 +142,117 @@ func infoPage(w http.ResponseWriter, url urlStruc) {

w.WriteHeader(200) w.Header().Set("Content-Type", "text/html") b, err := os.ReadFile("html/info.html") - if err != nil { - http500(w) + if hec(w, err, 500) { return } - custom := "no" - if url.isCustom { - custom = "yes" - } s := strings.ReplaceAll(string(b), "{{url}}", url.url) s = strings.ReplaceAll(s, "{{path}}", url.path) s = strings.ReplaceAll(s, "{{uses}}", intStr(url.uses)) - s = strings.ReplaceAll(s, "{{custom}}", custom) s = strings.ReplaceAll(s, "{{date}}", url.date.Format("2006-01-02 15:04:05")) w.Write([]byte(s)) } -func httpHandler(w http.ResponseWriter, r *http.Request) { - if len(r.URL.Path) > 1 { - path := strings.TrimSuffix(r.URL.Path[1:], "/") - var info bool = false - if strings.HasSuffix(path, "/info") { - info = true - path = strings.TrimSuffix(path, "/info") +func createPage(w http.ResponseWriter, api bool, code int, resp string) { + w.WriteHeader(code) + if api { + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(resp)) + return + } + s := "Error loading HTML\nYour request was still processed.\nRequest response: " + resp + "\n" + b, err := os.ReadFile("html/create.html") + if err != nil { + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(s)) + return + } + s = strings.ReplaceAll(string(b), "{{response}}", s) + if code != 200 { + s = strings.ReplaceAll(s, "{{title}}", "Your request failed:") + } else { + s = strings.ReplaceAll(s, "{{title}}", "Your short URL:") + } + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(s)) +} + +func createHandler(w http.ResponseWriter, r *http.Request) { + var err error + var url urlStruc + var api bool = false + url.date = time.Now() + url.uses = 0 + err = r.ParseForm() + if hec(w, err, 500) { + return + } + if r.Form.Get("api") != "" { + api = true + } + url.url = r.Form.Get("url") + if url.url == "" { + createPage(w, api, 400, "Error: No URL provided") + return + } + url.path = r.Form.Get("custom") + if url.path == "" { + url.path = getWord() + "-" + getWord() + "-" + for { + url.path += getWord() + if !checkURL(url.path) { + break + } + url.path += "-" } - url, err := readURL(path) - if err != nil || url.url == "" { - http404(w) + } else { + if checkURL(url.path) { + createPage(w, api, 400, "Error: Custom URL already exists") return } - if info { - infoPage(w, url) + } + err = writeURL(url) + if hec(w, err, 500) { + return + } + createPage(w, api, 200, r.URL.Host+"/"+url.path) +} + +func httpHandler(w http.ResponseWriter, r *http.Request) { + if len(r.URL.Path) < 2 { + if r.Method == "POST" { + createHandler(w, r) return } - http.Redirect(w, r, url.url, http.StatusFound) + http.ServeFile(w, r, "html/index.html") + } + path := strings.TrimSuffix(r.URL.Path[1:], "/") + var err error + if path == "favicon.ico" { + _, err = os.Stat("favicon.ico") + if hec(w, err, 404) { + return + } + http.ServeFile(w, r, "favicon.ico") + return + } + var info bool = false + if strings.HasSuffix(path, "/info") { + info = true + path = strings.TrimSuffix(path, "/info") + } + var url urlStruc + url, err = readURL(path) + if err != nil || url.url == "" { + http404(w) return } - if r.Method == "POST" { - + if info { + infoPage(w, url) + return } + http.Redirect(w, r, url.url, http.StatusFound) + url.uses++ + _ = writeURL(url) } func main() {