From fd533c3f3ba048a30c69afa2cd40dc77061c25b4 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 26 Nov 2024 21:38:49 -0600 Subject: [PATCH] Mostly working UI --- main.go | 37 +++++++- static/index.html | 221 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 static/index.html diff --git a/main.go b/main.go index 20d5942..43f2f94 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" _ "github.com/lib/pq" ) @@ -16,6 +17,10 @@ type ShortLinks struct { mux *http.ServeMux } +type response struct { + Short string `json:"short"` +} + func NewShortLinks() (*ShortLinks, error) { tmpl := template.New("index.html") @@ -29,7 +34,9 @@ func NewShortLinks() (*ShortLinks, error) { mux: http.NewServeMux(), } - sl.mux.HandleFunc("/", sl.serveRoot) + sl.mux.HandleFunc("GET /{$}", sl.serveRoot) + sl.mux.HandleFunc("GET /{short}", sl.serveShort) + sl.mux.HandleFunc("POST /{$}", sl.serveSet) return sl, nil } @@ -48,6 +55,34 @@ func (sl *ShortLinks) serveRoot(w http.ResponseWriter, r *http.Request) { } } +func (sl *ShortLinks) serveShort(w http.ResponseWriter, r *http.Request) { + sendError(w, http.StatusNotImplemented, "not implemented") +} + +func (sl *ShortLinks) serveSet(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + sendError(w, http.StatusBadRequest, "Parse form: %s", err) + return + } + + log.Printf("%s %s %s", r.RemoteAddr, r.URL.Path, r.Form.Encode()) + + short := r.Form.Get("short") + + long := r.Form.Get("long") + if long == "" { + sendError(w, http.StatusBadRequest, "long= param required") + return + } + + time.Sleep(1 * time.Second) + + sendJSON(w, response{ + Short: short, + }) +} + func main() { port := os.Getenv("PORT") if port == "" { diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..b044950 --- /dev/null +++ b/static/index.html @@ -0,0 +1,221 @@ + + + + + + + + + + + +
+ + + +
+ + + + +
+
+ +
+ Set +
+ +
+
+ + + +
+ +
+ +
+
+ + + +
+ +