More sensible package structure
This commit is contained in:
parent
e9e997c405
commit
4f250077c8
27
back/genstring.go
Normal file
27
back/genstring.go
Normal 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
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package henwen
|
package back
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -1,14 +1,9 @@
|
|||||||
package main
|
package front
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"math/big"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
@ -16,7 +11,7 @@ import (
|
|||||||
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||||
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
||||||
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
|
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
|
||||||
"gitlab.codemonkeysoftware.net/b/henwen"
|
"gitlab.codemonkeysoftware.net/b/henwen/back"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -28,14 +23,14 @@ const (
|
|||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
store *henwen.Store
|
store *back.Store
|
||||||
title string
|
title string
|
||||||
baseURL string
|
baseURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandlerParams struct {
|
type HandlerParams struct {
|
||||||
Title string
|
Title string
|
||||||
Store *henwen.Store
|
Store *back.Store
|
||||||
BaseURL string
|
BaseURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +98,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
description := r.FormValue(fieldNameDescription)
|
description := r.FormValue(fieldNameDescription)
|
||||||
|
|
||||||
event, err := h.store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{
|
||||||
Name: eventName,
|
Name: eventName,
|
||||||
Description: description,
|
Description: description,
|
||||||
EarliestDate: earliest,
|
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) {
|
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
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),
|
AlphaID: query.Get(keyEventID),
|
||||||
AdminCode: query.Get(keyAdminCode),
|
AdminCode: query.Get(keyAdminCode),
|
||||||
})
|
})
|
||||||
@ -218,47 +213,4 @@ const (
|
|||||||
const keyEventID = "event_id"
|
const keyEventID = "event_id"
|
||||||
const keyAdminCode = "admin_code"
|
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"
|
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
33
main.go
Normal 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())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user