migrate to chi for routing
This commit is contained in:
@@ -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,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
|
||||
)
|
||||
|
||||
@@ -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
@@ -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)
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
package shared
|
||||
|
||||
templ Layout() {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Comfychan</title>
|
||||
<script src="https://unpkg.com/hyperscript.org@0.9.14"></script>
|
||||
<link rel="stylesheet" href="/static/reset.css"/>
|
||||
<link rel="stylesheet" href="/static/index.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<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>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user