got tailwind and static file server working

This commit is contained in:
2025-02-23 19:15:42 -06:00
parent c5d3538e60
commit 6445f3a495
7 changed files with 373 additions and 9 deletions
+22 -1
View File
@@ -1,20 +1,41 @@
package main
import (
"embed"
"io/fs"
"mmaschedule-go/event"
"net/http"
"time"
)
//go:generate bun run build
//go:generate templ generate
func RunServer(addr string, queries *event.Queries) error {
mux := http.NewServeMux()
state := ServerState{queries: queries}
static, err := StaticHandler("/static/")
if err != nil {
return err
}
mux.HandleFunc("GET /", state.HandleFunc(RouteIndex))
mux.HandleFunc("GET /events/{slug}/", state.HandleFunc(RouteEvent))
err := http.ListenAndServe(addr, mux)
mux.Handle("GET /static/", static)
err = http.ListenAndServe(addr, mux)
return err
}
//go:embed static/*
var staticFiles embed.FS
func StaticHandler(prefix string) (http.Handler, error) {
static, err := fs.Sub(staticFiles, "static")
if err != nil {
return nil, err
}
return http.StripPrefix(prefix, http.FileServer(http.FS(static))), nil
}
func RouteIndex(w http.ResponseWriter, r *http.Request, q *event.Queries) {
slug, err := q.GetUpcomingEvent(r.Context(), time.Now().Unix())
if err != nil {