added some basic html files bug fixes
sophuwu sophie@sophuwu.site
Fri, 01 Sep 2023 05:13:33 +0100
5 files changed,
72 insertions(+),
27 deletions(-)
A
html/create.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>URL Shortner</title> +</head> +<body> + <h1>URL Shortner</h1> + <p>{{title}}</p> + <p>{{response}}</p> +</body> +</html>
A
html/index.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>URL Shortner</title> +</head> +<body> + <h1>URL Shortner</h1> + <form action="/" method="post"> + <input type="text" name="url" placeholder="Enter a URL here" autocomplete="off"> + <input type="submit" value="Shorten"> + </form> +</body> +</html>
A
html/info.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>URL Shotner</title> +</head> +<body> + <h1>URL Shortner</h1> + <p>Alias: {{path}}</p> + <p>Destination: <a href="{{url}}">{{url}}</a></p> + <p>Created: {{date}}</p> + <p>Uses: {{uses}}</p> +</body> +</html>
M
main.go
→
main.go
@@ -17,10 +17,10 @@ var urlDB *bolt.DB
var wordList []string type urlStruc struct { - 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@@ -69,7 +69,7 @@ }
func checkURL(words string) bool { url, err := readURL(words) - if err == nil && url.url != "" { + if err == nil && url.Url != "" { return true } return false@@ -82,7 +82,7 @@ return err
} return urlDB.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("urls")) - return b.Put([]byte(url.path), jsonUrl) + return b.Put([]byte(url.Path), jsonUrl) }) }@@ -147,10 +147,10 @@ b, err := os.ReadFile("html/info.html")
if hec(w, err, 500) { return } - 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, "{{date}}", url.date.Format("2006-01-02 15:04:05")) + 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, "{{date}}", url.Date.Format("2006-01-02 15:04:05")) w.Write([]byte(s)) }@@ -168,7 +168,7 @@ w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(s)) return } - s = strings.ReplaceAll(string(b), "{{response}}", s) + s = strings.ReplaceAll(string(b), "{{response}}", resp) if code != 200 { s = strings.ReplaceAll(s, "{{title}}", "Your request failed:") } else {@@ -182,8 +182,8 @@ 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 + url.Date = time.Now() + url.Uses = 0 err = r.ParseForm() if hec(w, err, 500) { return@@ -191,23 +191,23 @@ }
if r.Form.Get("mode") == "api" { api = true } - url.url = r.Form.Get("url") - if url.url == "" { + 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() + "-" + url.Path = r.Form.Get("custom") + if url.Path == "" { + url.Path = getWord() + "-" + getWord() + "-" for { - url.path += getWord() - if !checkURL(url.path) { + url.Path += getWord() + if !checkURL(url.Path) { break } - url.path += "-" + url.Path += "-" } } else { - if checkURL(url.path) { + if checkURL(url.Path) { createPage(w, api, 400, "Error: Custom URL already exists") return }@@ -216,7 +216,7 @@ err = writeURL(url)
if hec(w, err, 500) { return } - createPage(w, api, 200, r.URL.Host+"/"+url.path) + createPage(w, api, 200, r.URL.Host+"/"+url.Path) } func httpHandler(w http.ResponseWriter, r *http.Request) {@@ -228,7 +228,9 @@ }
http.ServeFile(w, r, "html/index.html") return } - path := strings.TrimSuffix(r.URL.Path[1:], "/") + path := strings.TrimSuffix(r.URL.Path, "/") + path = strings.TrimPrefix(path, r.URL.Host) + path = strings.TrimPrefix(path, "/") var err error if path == "favicon.ico" { _, err = os.Stat("favicon.ico")@@ -245,7 +247,7 @@ path = strings.TrimSuffix(path, "/info")
} var url urlStruc url, err = readURL(path) - if err != nil || url.url == "" { + if err != nil || url.Url == "" { http404(w) return }@@ -253,8 +255,8 @@ if info {
infoPage(w, url) return } - http.Redirect(w, r, url.url, http.StatusFound) - url.uses++ + http.Redirect(w, r, url.Url, http.StatusFound) + url.Uses++ _ = writeURL(url) }@@ -293,4 +295,5 @@ <-quit
urlDB.Close() server.Shutdown(context.Background()) + print("\nServer stopped\n\n") }