migrate to chi for routing

This commit is contained in:
Dominic Ferrando
2025-04-17 20:49:08 -04:00
parent 0fc22e8b96
commit 0146a9583d
5 changed files with 55 additions and 38 deletions
+1 -1
View File
@@ -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 \
+4 -1
View File
@@ -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
)
+2
View File
@@ -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=
+19 -10
View File
@@ -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)
}
+4 -1
View File
@@ -3,6 +3,7 @@ package shared
templ Layout() {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Comfychan</title>
@@ -10,6 +11,7 @@ templ Layout() {
<link rel="stylesheet" href="/static/reset.css" />
<link rel="stylesheet" href="/static/index.css" />
</head>
<body>
<div id="boardList">
<span>
@@ -20,11 +22,12 @@ templ Layout() {
<span>
[
<a href="/comfy">comfy</a> /
<a href="/r9k">r9k</a>
<a href="/g">g</a>
]
</span>
</div>
{ children... }
</body>
</html>
}