Mostly working UI

This commit is contained in:
Ian Gulliver
2024-11-26 21:38:49 -06:00
parent 3eb8785440
commit fd533c3f3b
2 changed files with 257 additions and 1 deletions

37
main.go
View File

@@ -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 == "" {