Create handler type
This commit is contained in:
parent
da4cbc4c50
commit
d07d5cac31
@ -33,6 +33,59 @@ const (
|
||||
|
||||
var store *henwen.Store
|
||||
|
||||
type handler struct {
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
func NewHandler() http.Handler {
|
||||
h := &handler{
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
h.mux.HandleFunc(pathRoot, h.handleRoot)
|
||||
h.mux.HandleFunc(pathCreate, h.handleCreate)
|
||||
h.mux.HandleFunc(pathDoCreate, h.handleDoCreate)
|
||||
h.mux.HandleFunc(pathAdmin, h.handleAdmin)
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||
_ = writePage(w, pageRoot())
|
||||
}
|
||||
|
||||
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
|
||||
_ = writePage(w, pageCreate())
|
||||
}
|
||||
|
||||
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
||||
earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest))
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "bad earliest date")
|
||||
return
|
||||
}
|
||||
latest, err := time.Parse(dateFmt, r.FormValue(fieldNameLatest))
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "bad latest date")
|
||||
return
|
||||
}
|
||||
eventName := r.FormValue(fieldNameEventName)
|
||||
if eventName == "" {
|
||||
fmt.Fprint(w, "event name is required")
|
||||
return
|
||||
}
|
||||
description := r.FormValue(fieldNameDescription)
|
||||
|
||||
_ = writePage(w, pageDoCreate(eventName, description, earliest, latest))
|
||||
}
|
||||
|
||||
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
_ = writePage(w, pageAdmin(query.Get(keyEventID), query.Get(keyAdminCode)))
|
||||
}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func writePage(w io.Writer, contents h.Term) error {
|
||||
page := e.Html()(
|
||||
e.Head()(
|
||||
@ -196,41 +249,9 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc(pathRoot, func(w http.ResponseWriter, r *http.Request) {
|
||||
_ = writePage(w, pageRoot())
|
||||
})
|
||||
mux.HandleFunc(pathCreate, func(w http.ResponseWriter, r *http.Request) {
|
||||
_ = writePage(w, pageCreate())
|
||||
})
|
||||
mux.HandleFunc(pathDoCreate, func(w http.ResponseWriter, r *http.Request) {
|
||||
earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest))
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "bad earliest date")
|
||||
return
|
||||
}
|
||||
latest, err := time.Parse(dateFmt, r.FormValue(fieldNameLatest))
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "bad latest date")
|
||||
return
|
||||
}
|
||||
eventName := r.FormValue(fieldNameEventName)
|
||||
if eventName == "" {
|
||||
fmt.Fprint(w, "event name is required")
|
||||
return
|
||||
}
|
||||
description := r.FormValue(fieldNameDescription)
|
||||
|
||||
_ = writePage(w, pageDoCreate(eventName, description, earliest, latest))
|
||||
})
|
||||
mux.HandleFunc(pathAdmin, func(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
_ = writePage(w, pageAdmin(query.Get(keyEventID), query.Get(keyAdminCode)))
|
||||
})
|
||||
|
||||
srv := http.Server{
|
||||
Addr: Addr,
|
||||
Handler: mux,
|
||||
Handler: NewHandler(),
|
||||
}
|
||||
log.Println(srv.ListenAndServe())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user