From 4f250077c8b2b8c484b5cb89be04ef6158211aed Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 1 Apr 2020 00:35:15 -0600 Subject: [PATCH] More sensible package structure --- back/genstring.go | 27 ++++++++++++++++++ store.go => back/store.go | 2 +- {http => front}/server.go | 60 ++++----------------------------------- main.go | 33 +++++++++++++++++++++ 4 files changed, 67 insertions(+), 55 deletions(-) create mode 100644 back/genstring.go rename store.go => back/store.go (99%) rename {http => front}/server.go (77%) create mode 100644 main.go diff --git a/back/genstring.go b/back/genstring.go new file mode 100644 index 0000000..7cdf97c --- /dev/null +++ b/back/genstring.go @@ -0,0 +1,27 @@ +package back + +import ( + "bytes" + "crypto/rand" + "math/big" +) + +var chars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + +func SecureGenString(length int) (string, error) { + charsLength := big.NewInt(int64(len(chars))) + var maxN big.Int + maxN.Exp(charsLength, big.NewInt(int64(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 +} diff --git a/store.go b/back/store.go similarity index 99% rename from store.go rename to back/store.go index c8e9402..b80a3ef 100644 --- a/store.go +++ b/back/store.go @@ -1,4 +1,4 @@ -package henwen +package back import ( "context" diff --git a/http/server.go b/front/server.go similarity index 77% rename from http/server.go rename to front/server.go index 1bd5cf0..8b7bff5 100644 --- a/http/server.go +++ b/front/server.go @@ -1,14 +1,9 @@ -package main +package front import ( - "bytes" "context" - "crypto/rand" - "flag" "fmt" "io" - "log" - "math/big" "net/http" "net/url" "time" @@ -16,7 +11,7 @@ import ( hm "gitlab.codemonkeysoftware.net/b/hatmill" a "gitlab.codemonkeysoftware.net/b/hatmill/attribute" e "gitlab.codemonkeysoftware.net/b/hatmill/element" - "gitlab.codemonkeysoftware.net/b/henwen" + "gitlab.codemonkeysoftware.net/b/henwen/back" ) const ( @@ -28,14 +23,14 @@ const ( type handler struct { mux *http.ServeMux - store *henwen.Store + store *back.Store title string baseURL string } type HandlerParams struct { Title string - Store *henwen.Store + Store *back.Store BaseURL string } @@ -103,7 +98,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) { } description := r.FormValue(fieldNameDescription) - event, err := h.store.CreateEvent(context.Background(), henwen.CreateEventCommand{ + event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{ Name: eventName, Description: description, EarliestDate: earliest, @@ -145,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 := h.store.GetEvent(context.Background(), henwen.GetEventQuery{ + event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{ AlphaID: query.Get(keyEventID), AdminCode: query.Get(keyAdminCode), }) @@ -218,47 +213,4 @@ const ( const keyEventID = "event_id" const keyAdminCode = "admin_code" -var chars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - -func genString(length int) (string, error) { - charsLength := big.NewInt(int64(len(chars))) - var maxN big.Int - maxN.Exp(charsLength, big.NewInt(int64(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 -} - const formDateLayout = "2006-01-02" - -func main() { - 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(HandlerParams{ - Store: store, - Title: *title, - BaseURL: *baseURL, - }), - } - log.Println(srv.ListenAndServe()) -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..8e9f0c8 --- /dev/null +++ b/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "log" + "net/http" + + "gitlab.codemonkeysoftware.net/b/henwen/back" + "gitlab.codemonkeysoftware.net/b/henwen/front" +) + +func main() { + 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 := back.NewStore(*dbFileName, back.SecureGenString) + if err != nil { + log.Fatal(err) + } + + srv := http.Server{ + Addr: *addr, + Handler: front.NewHandler(front.HandlerParams{ + Store: store, + Title: *title, + BaseURL: *baseURL, + }), + } + log.Println(srv.ListenAndServe()) +}