small improvements

This commit is contained in:
bronku 2026-06-11 09:10:27 +02:00
parent bb32802324
commit 5f2d6f00bc
10 changed files with 117 additions and 178 deletions

View file

@ -4,52 +4,44 @@ import (
"embed"
"net/http"
"github.com/a-h/templ"
"git.bronku.xyz/bronku/cake-order-tracker/store"
)
type Server struct {
s *store.Store
routes map[string]route
http.Handler
}
type fetcher func(r *http.Request) (templ.Component, int, error)
type route struct {
function fetcher
type Server struct {
s *store.Store
http.Handler
}
//go:embed static/*
var static embed.FS
func (h *Server) loadHandler() {
mux := http.NewServeMux()
for i, e := range h.routes {
mux.HandleFunc(i, h.render(e.function))
func redirect(path string, code int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, code)
}
mux.HandleFunc("GET /", h.redirect("/orders", http.StatusSeeOther))
fs := http.FileServerFS(static)
mux.Handle("GET /static/", fs)
h.Handler = mux
}
func New(store *store.Store) *Server {
var server Server
server.routes = map[string]route{
"GET /order/": {server.order},
"GET /orders": {server.orders},
"GET /orders/search/": {server.ordersSearch},
"GET /cake/": {server.cake},
"GET /cakes": {server.cakes},
"POST /order/": {server.postOrder},
"POST /cake/": {server.postCake},
}
server.s = store
server.loadHandler()
mux := http.NewServeMux()
mux.HandleFunc("GET /", redirect("/orders", http.StatusSeeOther))
mux.HandleFunc("GET /order/", server.render(server.order))
mux.HandleFunc("GET /orders", server.render(server.orders))
mux.HandleFunc("GET /orders/search/", server.render(server.ordersSearch))
mux.HandleFunc("GET /cake/", server.render(server.cake))
mux.HandleFunc("GET /cakes", server.render(server.cakes))
mux.HandleFunc("POST /order/", server.render(server.postOrder))
mux.HandleFunc("POST /cake/", server.render(server.postCake))
fs := http.FileServerFS(static)
mux.Handle("GET /static/", fs)
server.Handler = mux
return &server
}