updated read and write function to support struct started http handler
sophuwu sophie@sophuwu.site
Thu, 31 Aug 2023 21:58:44 +0100
1 files changed,
62 insertions(+),
8 deletions(-)
jump to
M
main.go
→
main.go
@@ -1,14 +1,26 @@
package main import ( + "encoding/json" bolt "go.etcd.io/bbolt" "math/rand" + "net/http" "os" "strings" + "time" ) var urlDB *bolt.DB var wordList []string + +type urlStruc struct { + isCustom bool + creator string + date time.Time + path string + url string + uses int +} func fec(err error) { // fatal error check if err != nil {@@ -44,21 +56,63 @@ }
return word() + "-" + word() + "-" + word() } -func readURL(words string) string { - var url string - _ = urlDB.View(func(tx *bolt.Tx) error { +func readURL(words string) (urlStruc, error) { + var url urlStruc + var err error + err = urlDB.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("urls")) - url = string(b.Get([]byte(words))) - return nil + return json.Unmarshal(b.Get([]byte(words)), &url) }) - return url + return url, err +} + +func checkURL(words string) bool { + url, err := readURL(words) + if err == nil && url.url != "" { + return true + } + return false } -func writeURL(words, url string) error { +func writeURL(url urlStruc) error { + jsonUrl, err := json.Marshal(url) + if err != nil { + return err + } return urlDB.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("urls")) - return b.Put([]byte(words), []byte(url)) + return b.Put([]byte(url.path), jsonUrl) }) +} + +func http404(w http.ResponseWriter) { + w.WriteHeader(404) + b, err := os.ReadFile("404.html") + if err != nil { + b = []byte("404 not found") + w.Header().Set("Content-Type", "text/plain") + } else { + w.Header().Set("Content-Type", "text/html") + } + w.Write(b) +} + +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") + } + url, err := readURL(path) + if url != "" { + http.Redirect(w, r, url, http.StatusFound) + return + } + http.Error(w, "404 not found", http.StatusNotFound) + return + } } func main() {