Move Store instance into handler

This commit is contained in:
Brandon Dyck 2020-03-31 23:52:29 -06:00
parent d0937474b5
commit 5feda1dd9f

View File

@ -31,14 +31,14 @@ const (
pathAdmin = "/admin" pathAdmin = "/admin"
) )
var store *henwen.Store
type handler struct { type handler struct {
mux *http.ServeMux mux *http.ServeMux
store *henwen.Store
} }
func NewHandler() http.Handler { func NewHandler(store *henwen.Store) http.Handler {
h := &handler{ h := &handler{
store: store,
mux: http.NewServeMux(), mux: http.NewServeMux(),
} }
h.mux.HandleFunc(pathRoot, h.handleRoot) h.mux.HandleFunc(pathRoot, h.handleRoot)
@ -98,7 +98,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
} }
description := r.FormValue(fieldNameDescription) description := r.FormValue(fieldNameDescription)
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{ event, err := h.store.CreateEvent(context.Background(), henwen.CreateEventCommand{
Name: eventName, Name: eventName,
Description: description, Description: description,
EarliestDate: earliest, EarliestDate: earliest,
@ -140,7 +140,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) { func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
event, err := store.GetEvent(context.Background(), henwen.GetEventQuery{ event, err := h.store.GetEvent(context.Background(), henwen.GetEventQuery{
AlphaID: query.Get(keyEventID), AlphaID: query.Get(keyEventID),
AdminCode: query.Get(keyAdminCode), AdminCode: query.Get(keyAdminCode),
}) })
@ -237,15 +237,14 @@ func genString() (string, error) {
const dateFmt = "2006-01-02" const dateFmt = "2006-01-02"
func main() { func main() {
var err error store, err := henwen.NewStore(dbFileName, genString)
store, err = henwen.NewStore(dbFileName, genString)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
srv := http.Server{ srv := http.Server{
Addr: Addr, Addr: Addr,
Handler: NewHandler(), Handler: NewHandler(store),
} }
log.Println(srv.ListenAndServe()) log.Println(srv.ListenAndServe())
} }