Move config constants into environment variables

This commit is contained in:
Brandon Dyck 2020-04-01 00:09:49 -06:00
parent 5feda1dd9f
commit 3504ecd196

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/rand" "crypto/rand"
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -19,11 +20,6 @@ import (
_ "gitlab.codemonkeysoftware.net/b/henwen" _ "gitlab.codemonkeysoftware.net/b/henwen"
) )
const Addr = ":8080"
const Title = "Henwen"
const baseURL = "http://localhost:8080"
const dbFileName = "./henwen.db"
const ( const (
pathRoot = "/" pathRoot = "/"
pathCreate = "/create" pathCreate = "/create"
@ -32,14 +28,24 @@ const (
) )
type handler struct { type handler struct {
mux *http.ServeMux mux *http.ServeMux
store *henwen.Store store *henwen.Store
title string
baseURL string
} }
func NewHandler(store *henwen.Store) http.Handler { type HandlerParams struct {
Title string
Store *henwen.Store
BaseURL string
}
func NewHandler(params HandlerParams) http.Handler {
h := &handler{ h := &handler{
store: store, store: params.Store,
mux: http.NewServeMux(), title: params.Title,
baseURL: params.BaseURL,
mux: http.NewServeMux(),
} }
h.mux.HandleFunc(pathRoot, h.handleRoot) h.mux.HandleFunc(pathRoot, h.handleRoot)
h.mux.HandleFunc(pathCreate, h.handleCreate) h.mux.HandleFunc(pathCreate, h.handleCreate)
@ -55,7 +61,7 @@ func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
hm.Text("Create event"), hm.Text("Create event"),
), ),
} }
_ = writePage(w, body) _ = h.writePage(w, body)
} }
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) { func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
@ -77,7 +83,7 @@ func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
e.Input(a.Type("submit")), e.Input(a.Type("submit")),
), ),
} }
_ = writePage(w, body) _ = h.writePage(w, body)
} }
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) { func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
@ -111,7 +117,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
var adminQuery = make(url.Values) var adminQuery = make(url.Values)
adminQuery.Add(keyEventID, event.AlphaID) adminQuery.Add(keyEventID, event.AlphaID)
adminQuery.Add(keyAdminCode, event.AdminCode) adminQuery.Add(keyAdminCode, event.AdminCode)
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode() adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
const dateDisplayFmt = "Monday, January 2, 2006" const dateDisplayFmt = "Monday, January 2, 2006"
@ -135,7 +141,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
e.H3()(hm.Text("Latest date")), e.H3()(hm.Text("Latest date")),
hm.Text(latest.Format(dateDisplayFmt)), hm.Text(latest.Format(dateDisplayFmt)),
} }
_ = writePage(w, body) _ = h.writePage(w, body)
} }
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) { func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
@ -182,20 +188,20 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input(a.Type("submit")), e.Input(a.Type("submit")),
), ),
} }
_ = writePage(w, body) _ = h.writePage(w, body)
} }
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r) h.mux.ServeHTTP(w, r)
} }
func writePage(w io.Writer, contents hm.Term) error { func (h *handler) writePage(w io.Writer, contents hm.Term) error {
page := e.Html()( page := e.Html()(
e.Head()( e.Head()(
e.Title()(hm.Text(Title)), e.Title()(hm.Text(h.title)),
), ),
e.Body()( e.Body()(
e.H1()(hm.Text(Title)), e.H1()(hm.Text(h.title)),
e.Div()(contents), e.Div()(contents),
), ),
) )
@ -237,14 +243,24 @@ func genString() (string, error) {
const dateFmt = "2006-01-02" const dateFmt = "2006-01-02"
func main() { func main() {
store, err := henwen.NewStore(dbFileName, genString) addr := flag.String("address", ":8080", "bind address for HTTP server as host:port")
title := flag.String("title", "Henwen", "website title")
baseURL := flag.String("baseURL", "http://localhost:8080", "base URL for HTTP routes")
dbFileName := flag.String("db", "./henwen.db", "name of database file")
flag.Parse()
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(store), Handler: NewHandler(HandlerParams{
Store: store,
Title: *title,
BaseURL: *baseURL,
}),
} }
log.Println(srv.ListenAndServe()) log.Println(srv.ListenAndServe())
} }