2020-03-24 14:59:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-31 12:58:44 +00:00
|
|
|
"bytes"
|
2020-03-31 14:47:40 +00:00
|
|
|
"context"
|
2020-03-31 12:58:44 +00:00
|
|
|
"crypto/rand"
|
2020-03-24 14:59:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2020-03-31 12:58:44 +00:00
|
|
|
"math/big"
|
2020-03-24 14:59:27 +00:00
|
|
|
"net/http"
|
2020-03-31 14:47:40 +00:00
|
|
|
"net/url"
|
2020-03-24 14:59:27 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
h "gitlab.codemonkeysoftware.net/b/hatmill"
|
|
|
|
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
|
|
|
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
|
2020-03-31 12:58:44 +00:00
|
|
|
"gitlab.codemonkeysoftware.net/b/henwen"
|
2020-03-24 14:59:27 +00:00
|
|
|
_ "gitlab.codemonkeysoftware.net/b/henwen"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Addr = ":8080"
|
|
|
|
const Title = "Henwen"
|
2020-03-31 12:58:44 +00:00
|
|
|
const baseURL = "http://localhost:8080"
|
|
|
|
const dbFileName = "./henwen.db"
|
2020-03-24 14:59:27 +00:00
|
|
|
|
|
|
|
const (
|
2020-03-31 14:47:40 +00:00
|
|
|
pathRoot = "/"
|
|
|
|
pathCreate = "/create"
|
|
|
|
pathDoCreate = "/create/do"
|
|
|
|
pathAdmin = "/admin"
|
2020-03-24 14:59:27 +00:00
|
|
|
)
|
|
|
|
|
2020-03-31 12:58:44 +00:00
|
|
|
var store *henwen.Store
|
|
|
|
|
2020-04-01 05:31:30 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-24 14:59:27 +00:00
|
|
|
func writePage(w io.Writer, contents h.Term) error {
|
|
|
|
page := e.Html()(
|
|
|
|
e.Head()(
|
|
|
|
e.Title()(h.Text(Title)),
|
|
|
|
),
|
|
|
|
e.Body()(
|
|
|
|
e.H1()(h.Text(Title)),
|
|
|
|
e.Div()(contents),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
_, err := h.WriteDocument(w, page)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func pageRoot() h.Term {
|
|
|
|
return h.Terms{
|
|
|
|
e.H2()(h.Text("Welcome!")),
|
2020-03-31 14:47:40 +00:00
|
|
|
e.A(a.Href(pathCreate))(
|
2020-03-24 14:59:27 +00:00
|
|
|
h.Text("Create event"),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-04-01 04:36:41 +00:00
|
|
|
fieldNameEarliest = "earliestDate"
|
|
|
|
fieldNameLatest = "latestDate"
|
|
|
|
fieldNameEventName = "eventName"
|
|
|
|
fieldNameDescription = "eventDescription"
|
2020-03-24 14:59:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pageCreate() h.Term {
|
|
|
|
return h.Terms{
|
|
|
|
e.H2()(h.Text("Create an event")),
|
2020-04-01 04:36:41 +00:00
|
|
|
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
|
2020-03-24 14:59:27 +00:00
|
|
|
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
|
|
|
e.Input(a.Name(fieldNameEventName)),
|
|
|
|
|
2020-04-01 04:36:41 +00:00
|
|
|
e.Label(a.For(fieldNameDescription))(h.Text("Description")),
|
|
|
|
e.Textarea(a.Name(fieldNameEventName), a.Placeholder("What's going on?"))(),
|
|
|
|
|
2020-03-24 14:59:27 +00:00
|
|
|
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
|
|
|
e.Input(a.Name(fieldNameEarliest), a.Type("date")),
|
|
|
|
|
|
|
|
e.Label(a.For(fieldNameLatest))(h.Text("Latest date")),
|
|
|
|
e.Input(a.Name(fieldNameLatest), a.Type("date")),
|
|
|
|
|
|
|
|
e.Input(a.Type("submit")),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 04:36:41 +00:00
|
|
|
func pageAdmin(alphaID, adminCode string) h.Term {
|
|
|
|
event, err := store.GetEvent(context.Background(), henwen.GetEventQuery{
|
|
|
|
AlphaID: alphaID,
|
|
|
|
AdminCode: adminCode,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return h.Text(err.Error())
|
|
|
|
}
|
|
|
|
return h.Terms{
|
|
|
|
e.H2()(h.Text("Edit your event")),
|
|
|
|
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
|
|
|
|
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
|
|
|
e.Input(
|
|
|
|
a.Name(fieldNameEventName),
|
|
|
|
a.Value(event.Name),
|
|
|
|
),
|
|
|
|
e.Br(),
|
|
|
|
|
|
|
|
e.Label(a.For(fieldNameDescription))(h.Text("Description")),
|
|
|
|
e.Textarea(a.Name(fieldNameEventName))(h.Text(event.Description)),
|
|
|
|
e.Br(),
|
|
|
|
|
|
|
|
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
|
|
|
e.Input(
|
|
|
|
a.Name(fieldNameEarliest),
|
|
|
|
a.Type("date"),
|
|
|
|
a.Value(event.EarliestDate.Format(dateFmt)),
|
|
|
|
),
|
|
|
|
e.Br(),
|
|
|
|
|
|
|
|
e.Label(a.For(fieldNameLatest))(h.Text("Latest date")),
|
|
|
|
e.Input(
|
|
|
|
a.Name(fieldNameLatest),
|
|
|
|
a.Type("date"),
|
|
|
|
a.Value(event.LatestDate.Format(dateFmt)),
|
|
|
|
),
|
|
|
|
e.Br(),
|
|
|
|
|
|
|
|
e.Input(a.Type("submit")),
|
|
|
|
),
|
|
|
|
}
|
2020-03-31 12:58:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 14:47:40 +00:00
|
|
|
const keyEventID = "event_id"
|
|
|
|
const keyAdminCode = "admin_code"
|
|
|
|
|
2020-04-01 04:36:41 +00:00
|
|
|
func pageDoCreate(name, description string, earliest, latest time.Time) h.Term {
|
2020-03-31 14:47:40 +00:00
|
|
|
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
|
|
|
Name: name,
|
2020-04-01 04:36:41 +00:00
|
|
|
Description: description,
|
2020-03-31 14:47:40 +00:00
|
|
|
EarliestDate: earliest,
|
|
|
|
LatestDate: latest,
|
|
|
|
})
|
2020-03-31 12:58:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return h.Text(err.Error())
|
|
|
|
}
|
2020-03-31 14:47:40 +00:00
|
|
|
var adminQuery = make(url.Values)
|
|
|
|
adminQuery.Add(keyEventID, event.AlphaID)
|
|
|
|
adminQuery.Add(keyAdminCode, event.AdminCode)
|
|
|
|
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
2020-03-31 12:58:44 +00:00
|
|
|
|
2020-04-01 04:36:41 +00:00
|
|
|
const dateDisplayFmt = "Monday, January 2, 2006"
|
|
|
|
|
2020-03-24 14:59:27 +00:00
|
|
|
return h.Terms{
|
|
|
|
e.H2()(h.Text("Created event!")),
|
2020-03-31 12:58:44 +00:00
|
|
|
e.P()(
|
|
|
|
h.Text("You can find it again at "),
|
|
|
|
e.A(a.Href(adminURL))(h.Text(adminURL)),
|
|
|
|
h.Text("."),
|
|
|
|
),
|
2020-03-24 14:59:27 +00:00
|
|
|
|
|
|
|
e.H3()(h.Text("Name")),
|
|
|
|
h.Text(name),
|
|
|
|
|
|
|
|
e.H3()(h.Text("Earliest date")),
|
2020-04-01 04:36:41 +00:00
|
|
|
h.Text(earliest.Format(dateDisplayFmt)),
|
2020-03-24 14:59:27 +00:00
|
|
|
|
|
|
|
e.H3()(h.Text("Latest date")),
|
2020-04-01 04:36:41 +00:00
|
|
|
h.Text(latest.Format(dateDisplayFmt)),
|
2020-03-24 14:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:58:44 +00:00
|
|
|
var chars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
|
|
|
|
func genString() (string, error) {
|
|
|
|
const length = 10
|
|
|
|
charsLength := big.NewInt(int64(len(chars)))
|
|
|
|
var maxN big.Int
|
|
|
|
maxN.Exp(charsLength, big.NewInt(length), nil)
|
|
|
|
n, err := rand.Int(rand.Reader, &maxN)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for n.Cmp(&big.Int{}) == 1 {
|
|
|
|
var charIdx big.Int
|
|
|
|
n.DivMod(n, charsLength, &charIdx)
|
|
|
|
_ = buf.WriteByte(chars[charIdx.Int64()])
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
2020-04-01 04:36:41 +00:00
|
|
|
const dateFmt = "2006-01-02"
|
|
|
|
|
2020-03-24 14:59:27 +00:00
|
|
|
func main() {
|
2020-03-31 14:47:40 +00:00
|
|
|
var err error
|
|
|
|
store, err = henwen.NewStore(dbFileName, genString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-24 14:59:27 +00:00
|
|
|
srv := http.Server{
|
|
|
|
Addr: Addr,
|
2020-04-01 05:31:30 +00:00
|
|
|
Handler: NewHandler(),
|
2020-03-24 14:59:27 +00:00
|
|
|
}
|
|
|
|
log.Println(srv.ListenAndServe())
|
|
|
|
}
|