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: 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: live/sync_assets:
go run github.com/air-verse/air@v1.61.7 \ 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/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 h1:6EqcJuGZW4OL+2iZ3MD+NnIcG7nGkaQeF2Zq5kf9ZGg=
github.com/a-h/templ v0.3.857/go.mod h1:qhrhAkRFubE7khxLZHsBFHfX+gWwVNKbzKeF9GlPV4M= 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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 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= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
+19 -10
View File
@@ -3,11 +3,14 @@ package main
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/dominicf2001/comfychan/internal/database"
"github.com/dominicf2001/comfychan/web/views"
_ "github.com/mattn/go-sqlite3"
"log" "log"
"net/http" "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 var dev = true
@@ -29,20 +32,26 @@ func main() {
} }
defer db.Close() defer db.Close()
http.Handle("/static/", r := chi.NewRouter()
r.Use(middleware.Logger)
r.Handle("/static/*",
disableCacheInDevMode( disableCacheInDevMode(
http.StripPrefix("/static", http.StripPrefix("/static",
http.FileServer(http.Dir("web/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) views.Index().Render(r.Context(), w)
}) })
http.HandleFunc("/comfy", func(w http.ResponseWriter, r *http.Request) { r.Get("/{slug}", func(w http.ResponseWriter, r *http.Request) {
board, err := database.GetBoard(db, "comfy") slug := chi.URLParam(r, "slug")
board, err := database.GetBoard(db, slug)
if err != nil { if err != nil {
http.Error(w, "Failed to load boards", http.StatusInternalServerError) http.NotFound(w, r)
log.Printf("Error fetching boards: %v", err) log.Printf("board %q not found: %v", slug, err)
return return
} }
@@ -50,5 +59,5 @@ func main() {
}) })
fmt.Println("Listening on :8080") fmt.Println("Listening on :8080")
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", r)
} }
+29 -26
View File
@@ -1,30 +1,33 @@
package shared package shared
templ Layout() { templ Layout() {
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<meta charset="UTF-8"/> <head>
<title>Comfychan</title> <meta charset="UTF-8" />
<script src="https://unpkg.com/hyperscript.org@0.9.14"></script> <title>Comfychan</title>
<link rel="stylesheet" href="/static/reset.css"/> <script src="https://unpkg.com/hyperscript.org@0.9.14"></script>
<link rel="stylesheet" href="/static/index.css"/> <link rel="stylesheet" href="/static/reset.css" />
</head> <link rel="stylesheet" href="/static/index.css" />
<body> </head>
<div id="boardList">
<span> <body>
[ <div id="boardList">
<a href="/">index</a> <span>
] [
</span> <a href="/">index</a>
<span> ]
[ </span>
<a href="/comfy">comfy</a> / <span>
<a href="/r9k">r9k</a> [
] <a href="/comfy">comfy</a> /
</span> <a href="/g">g</a>
</div> ]
{ children... } </span>
</body> </div>
</html> { children... }
</body>
</html>
} }