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,15 +31,15 @@ const (
pathAdmin = "/admin"
)
var store *henwen.Store
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{
mux: http.NewServeMux(),
store: store,
mux: http.NewServeMux(),
}
h.mux.HandleFunc(pathRoot, h.handleRoot)
h.mux.HandleFunc(pathCreate, h.handleCreate)
@ -98,7 +98,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
}
description := r.FormValue(fieldNameDescription)
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
event, err := h.store.CreateEvent(context.Background(), henwen.CreateEventCommand{
Name: eventName,
Description: description,
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) {
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),
AdminCode: query.Get(keyAdminCode),
})
@ -237,15 +237,14 @@ func genString() (string, error) {
const dateFmt = "2006-01-02"
func main() {
var err error
store, err = henwen.NewStore(dbFileName, genString)
store, err := henwen.NewStore(dbFileName, genString)
if err != nil {
log.Fatal(err)
}
srv := http.Server{
Addr: Addr,
Handler: NewHandler(),
Handler: NewHandler(store),
}
log.Println(srv.ListenAndServe())
}