diff --git a/Makefile b/Makefile index 7ed06a4..17c7c79 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ live/templ: - templ generate --watch --proxy="http://localhost:8080" --cmd="go run ./cmd" --open-browser=false -v + templ generate --watch --proxy="http://localhost:8080" --cmd="go run ." --open-browser=false -v live/sync_assets: go run github.com/air-verse/air@v1.61.7 \ diff --git a/go.mod b/go.mod index 4af0af5..1743f54 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,7 @@ go 1.23.8 require github.com/a-h/templ v0.3.857 -require github.com/mattn/go-sqlite3 v1.14.28 // indirect +require ( + github.com/go-chi/chi/v5 v5.2.1 // indirect + github.com/mattn/go-sqlite3 v1.14.28 // indirect +) diff --git a/go.sum b/go.sum index fb924ce..7289ee0 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/a-h/templ v0.3.857 h1:6EqcJuGZW4OL+2iZ3MD+NnIcG7nGkaQeF2Zq5kf9ZGg= github.com/a-h/templ v0.3.857/go.mod h1:qhrhAkRFubE7khxLZHsBFHfX+gWwVNKbzKeF9GlPV4M= +github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8= +github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= diff --git a/cmd/main.go b/main.go similarity index 65% rename from cmd/main.go rename to main.go index df83a64..8d71cdd 100644 --- a/cmd/main.go +++ b/main.go @@ -3,11 +3,14 @@ package main import ( "database/sql" "fmt" - "github.com/dominicf2001/comfychan/internal/database" - "github.com/dominicf2001/comfychan/web/views" - _ "github.com/mattn/go-sqlite3" "log" "net/http" + + "github.com/dominicf2001/comfychan/internal/database" + "github.com/dominicf2001/comfychan/web/views" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + _ "github.com/mattn/go-sqlite3" ) var dev = true @@ -29,20 +32,26 @@ func main() { } defer db.Close() - http.Handle("/static/", + r := chi.NewRouter() + + r.Use(middleware.Logger) + + r.Handle("/static/*", disableCacheInDevMode( http.StripPrefix("/static", http.FileServer(http.Dir("web/static"))))) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + r.Get("/", func(w http.ResponseWriter, r *http.Request) { views.Index().Render(r.Context(), w) }) - http.HandleFunc("/comfy", func(w http.ResponseWriter, r *http.Request) { - board, err := database.GetBoard(db, "comfy") + r.Get("/{slug}", func(w http.ResponseWriter, r *http.Request) { + slug := chi.URLParam(r, "slug") + + board, err := database.GetBoard(db, slug) if err != nil { - http.Error(w, "Failed to load boards", http.StatusInternalServerError) - log.Printf("Error fetching boards: %v", err) + http.NotFound(w, r) + log.Printf("board %q not found: %v", slug, err) return } @@ -50,5 +59,5 @@ func main() { }) fmt.Println("Listening on :8080") - http.ListenAndServe(":8080", nil) + http.ListenAndServe(":8080", r) } diff --git a/web/views/shared/layout.templ b/web/views/shared/layout.templ index bad6c89..262a7c0 100644 --- a/web/views/shared/layout.templ +++ b/web/views/shared/layout.templ @@ -1,30 +1,33 @@ package shared templ Layout() { - - - - - Comfychan - - - - - -
- - [ - index - ] - - - [ - comfy / - r9k - ] - -
- { children... } - - + + + + + + Comfychan + + + + + + +
+ + [ + index + ] + + + [ + comfy / + g + ] + +
+ { children... } + + + }