Mostly working UI
This commit is contained in:
37
main.go
37
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 == "" {
|
||||
|
||||
Reference in New Issue
Block a user