Move config constants into environment variables
This commit is contained in:
parent
5feda1dd9f
commit
3504ecd196
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@ -19,11 +20,6 @@ import (
|
||||
_ "gitlab.codemonkeysoftware.net/b/henwen"
|
||||
)
|
||||
|
||||
const Addr = ":8080"
|
||||
const Title = "Henwen"
|
||||
const baseURL = "http://localhost:8080"
|
||||
const dbFileName = "./henwen.db"
|
||||
|
||||
const (
|
||||
pathRoot = "/"
|
||||
pathCreate = "/create"
|
||||
@ -32,14 +28,24 @@ const (
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
mux *http.ServeMux
|
||||
store *henwen.Store
|
||||
mux *http.ServeMux
|
||||
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{
|
||||
store: store,
|
||||
mux: http.NewServeMux(),
|
||||
store: params.Store,
|
||||
title: params.Title,
|
||||
baseURL: params.BaseURL,
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
h.mux.HandleFunc(pathRoot, h.handleRoot)
|
||||
h.mux.HandleFunc(pathCreate, h.handleCreate)
|
||||
@ -55,7 +61,7 @@ func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||
hm.Text("Create event"),
|
||||
),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
_ = h.writePage(w, body)
|
||||
}
|
||||
|
||||
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")),
|
||||
),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
_ = h.writePage(w, body)
|
||||
}
|
||||
|
||||
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)
|
||||
adminQuery.Add(keyEventID, event.AlphaID)
|
||||
adminQuery.Add(keyAdminCode, event.AdminCode)
|
||||
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||
|
||||
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")),
|
||||
hm.Text(latest.Format(dateDisplayFmt)),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
_ = h.writePage(w, body)
|
||||
}
|
||||
|
||||
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")),
|
||||
),
|
||||
}
|
||||
_ = writePage(w, body)
|
||||
_ = h.writePage(w, body)
|
||||
}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
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()(
|
||||
e.Head()(
|
||||
e.Title()(hm.Text(Title)),
|
||||
e.Title()(hm.Text(h.title)),
|
||||
),
|
||||
e.Body()(
|
||||
e.H1()(hm.Text(Title)),
|
||||
e.H1()(hm.Text(h.title)),
|
||||
e.Div()(contents),
|
||||
),
|
||||
)
|
||||
@ -237,14 +243,24 @@ func genString() (string, error) {
|
||||
const dateFmt = "2006-01-02"
|
||||
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
srv := http.Server{
|
||||
Addr: Addr,
|
||||
Handler: NewHandler(store),
|
||||
Addr: *addr,
|
||||
Handler: NewHandler(HandlerParams{
|
||||
Store: store,
|
||||
Title: *title,
|
||||
BaseURL: *baseURL,
|
||||
}),
|
||||
}
|
||||
log.Println(srv.ListenAndServe())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user