cake-order-tracker/server/server.go
2026-06-11 09:10:27 +02:00

47 lines
1.1 KiB
Go

package server
import (
"embed"
"net/http"
"github.com/a-h/templ"
"git.bronku.xyz/bronku/cake-order-tracker/store"
)
type fetcher func(r *http.Request) (templ.Component, int, error)
type Server struct {
s *store.Store
http.Handler
}
//go:embed static/*
var static embed.FS
func redirect(path string, code int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, code)
}
}
func New(store *store.Store) *Server {
var server Server
server.s = store
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
}