More sensible package structure

This commit is contained in:
Brandon Dyck 2020-04-01 00:35:15 -06:00
parent e9e997c405
commit 4f250077c8
4 changed files with 67 additions and 55 deletions

27
back/genstring.go Normal file
View File

@ -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
}

View File

@ -1,4 +1,4 @@
package henwen
package back
import (
"context"

View File

@ -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())
}

33
main.go Normal file
View File

@ -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())
}