From e5b52712d19be405a36ad32612b89ab383fcf095 Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Thu, 17 Apr 2025 19:27:01 -0400 Subject: [PATCH] Implement dynamically getting board data --- .gitignore | 2 +- Makefile | 4 +- cmd/main.go | 23 ++++++++-- go.mod | 2 + go.sum | 2 + internal/database/comfychan.db | Bin 0 -> 24576 bytes internal/database/handlers.go | 34 +++++++++++++++ internal/database/models.go | 26 ++++++++++++ internal/{db => database}/seed.sql | 4 +- internal/db/db.go | 1 - internal/handlers.go | 1 - internal/models.go | 1 - web/views/{boards/comfy.templ => board.templ} | 14 +++--- .../{boards/comfy_templ.go => board_templ.go} | 40 +++++++++++++++--- web/views/shared/layout.templ | 5 +-- web/views/shared/layout_templ.go | 2 +- 16 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 internal/database/comfychan.db create mode 100644 internal/database/handlers.go create mode 100644 internal/database/models.go rename internal/{db => database}/seed.sql (95%) delete mode 100644 internal/db/db.go delete mode 100644 internal/handlers.go delete mode 100644 internal/models.go rename web/views/{boards/comfy.templ => board.templ} (71%) rename web/views/{boards/comfy_templ.go => board_templ.go} (70%) diff --git a/.gitignore b/.gitignore index a247e5e..c71612a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -internal/db/comfychan.db +internal/database/comfychan.db diff --git a/Makefile b/Makefile index c932ec9..7ed06a4 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,8 @@ live: make -j2 live/templ live/sync_assets db/seed: - sqlite3 ./internal/db/comfychan.db < ./internal/db/seed.sql + sqlite3 ./internal/database/comfychan.db < ./internal/database/seed.sql db/seed/f: - rm ./internal/db/comfychan.db && sqlite3 ./internal/db/comfychan.db < ./internal/db/seed.sql + rm ./internal/database/comfychan.db && sqlite3 ./internal/database/comfychan.db < ./internal/database/seed.sql diff --git a/cmd/main.go b/cmd/main.go index 8da5d7b..df83a64 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,11 +1,13 @@ package main import ( + "database/sql" "fmt" - "net/http" - + "github.com/dominicf2001/comfychan/internal/database" "github.com/dominicf2001/comfychan/web/views" - "github.com/dominicf2001/comfychan/web/views/boards" + _ "github.com/mattn/go-sqlite3" + "log" + "net/http" ) var dev = true @@ -21,6 +23,12 @@ func disableCacheInDevMode(next http.Handler) http.Handler { } func main() { + db, err := sql.Open("sqlite3", "internal/database/comfychan.db") + if err != nil { + log.Fatal(err) + } + defer db.Close() + http.Handle("/static/", disableCacheInDevMode( http.StripPrefix("/static", @@ -31,7 +39,14 @@ func main() { }) http.HandleFunc("/comfy", func(w http.ResponseWriter, r *http.Request) { - boards.Comfy().Render(r.Context(), w) + board, err := database.GetBoard(db, "comfy") + if err != nil { + http.Error(w, "Failed to load boards", http.StatusInternalServerError) + log.Printf("Error fetching boards: %v", err) + return + } + + views.Board(board).Render(r.Context(), w) }) fmt.Println("Listening on :8080") diff --git a/go.mod b/go.mod index 829ca38..4af0af5 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module github.com/dominicf2001/comfychan go 1.23.8 require github.com/a-h/templ v0.3.857 + +require github.com/mattn/go-sqlite3 v1.14.28 // indirect diff --git a/go.sum b/go.sum index 62aa53a..fb924ce 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,5 @@ 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/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= +github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/internal/database/comfychan.db b/internal/database/comfychan.db new file mode 100644 index 0000000000000000000000000000000000000000..0af4340fa22dbc46a6c39bcfcf00f2a93a637838 GIT binary patch literal 24576 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU|@q{1}I==U|?W@vOyFhh#-L381(9* zc=>-Yu=2z+@ayxR=6%51#}iLL*QoSp2#kinXb6mkz-S1JhQMeDjE2By2#kinPz-@4 z9aeU6adE~L^OD4*oYaE+;*w%8!R8#~>KNjx5aQ_Mk2o-IW#E9)h{F-Bp4jx=o)zjS%>|71F<1`h5T-{uQT>YF~gB2j6 z#TuC@nhO4Y3NEfbt|6`p&W^#(jxMfTngJ|q;@aAbk>J28F3ia+NsTW~Ei6sVOHM`O zvZ4eanxsZvVs5HVacZGvlMgeyxU@85OFr04sQpli1;q@gpn`@1ICPPM6?Z&9lqTgT z7U7A9;?kt7)Z`M>P=*JlIy5542=JuR+yV-s2$Kfgeh zk(EJQTr)X8H?7hcL^!1?fH^t}X+^1dT;TryM+W}y{2vFa??&A<8UmvsFd71*Aut*O zqaiRF0;3@?8UmvsFd71*Aut*Oqai?I2(U8qGo~|fFpG17M*%?d|BU?m8Tj{;XwIne z(GVC7fzc2c4S~@R7!85Z5Eu=C(GVC7fzc2c4S~@R7!3h>g@8RH8w01Yv2l26PI7*3 zszOP=fWwOUS@JCwj}`oC#hLo literal 0 HcmV?d00001 diff --git a/internal/database/handlers.go b/internal/database/handlers.go new file mode 100644 index 0000000..4977867 --- /dev/null +++ b/internal/database/handlers.go @@ -0,0 +1,34 @@ +package database + +import ( + "database/sql" +) + +func GetBoards(db *sql.DB) ([]Board, error) { + rows, err := db.Query(`select id, name, slug, tag from boards order by slug`) + if err != nil { + return nil, err + } + defer rows.Close() + + var result []Board + for rows.Next() { + var b Board + err := rows.Scan(&b.Id, &b.Name, &b.Slug, &b.Tag) + if err != nil { + return nil, err + } + result = append(result, b) + } + + return result, rows.Err() +} + +func GetBoard(db *sql.DB, slug string) (Board, error) { + row := db.QueryRow(`SELECT id, name, slug, tag FROM boards WHERE slug = ?`, slug) + + var result Board + row.Scan(&result.Id, &result.Name, &result.Slug, &result.Tag) + + return result, row.Err() +} diff --git a/internal/database/models.go b/internal/database/models.go new file mode 100644 index 0000000..2cf6c46 --- /dev/null +++ b/internal/database/models.go @@ -0,0 +1,26 @@ +package database + +import "time" + +type Board struct { + Id int + Slug string + Name string + Tag string +} + +type Thread struct { + Id int + BoardId int + Subject string + CreatedAt time.Time + BumpedAt time.Time +} + +type Post struct { + Id int + ThreadId int + Author string + Body string + CreatedAt time.Time +} diff --git a/internal/db/seed.sql b/internal/database/seed.sql similarity index 95% rename from internal/db/seed.sql rename to internal/database/seed.sql index 6fc9db1..97abe33 100644 --- a/internal/db/seed.sql +++ b/internal/database/seed.sql @@ -8,13 +8,13 @@ CREATE TABLE IF NOT EXISTS boards ( id INTEGER PRIMARY KEY, slug TEXT NOT NULL UNIQUE, name TEXT NOT NULL, - tag TEXT + tag TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS threads ( id INTEGER PRIMARY KEY AUTOINCREMENT, board_id INTEGER NOT NULL, - subject TEXT, + subject TEXT NOT NULL DEFAULT '', created_at DATETIME DEFAULT CURRENT_TIMESTAMP, bumped_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE diff --git a/internal/db/db.go b/internal/db/db.go deleted file mode 100644 index 3a49c63..0000000 --- a/internal/db/db.go +++ /dev/null @@ -1 +0,0 @@ -package db diff --git a/internal/handlers.go b/internal/handlers.go deleted file mode 100644 index 5bf0569..0000000 --- a/internal/handlers.go +++ /dev/null @@ -1 +0,0 @@ -package internal diff --git a/internal/models.go b/internal/models.go deleted file mode 100644 index 5bf0569..0000000 --- a/internal/models.go +++ /dev/null @@ -1 +0,0 @@ -package internal diff --git a/web/views/boards/comfy.templ b/web/views/board.templ similarity index 71% rename from web/views/boards/comfy.templ rename to web/views/board.templ index 25c7d96..36ca680 100644 --- a/web/views/boards/comfy.templ +++ b/web/views/board.templ @@ -1,6 +1,10 @@ -package boards +package views -import "github.com/dominicf2001/comfychan/web/views/shared" +import ( + "fmt" + database "github.com/dominicf2001/comfychan/internal/database" + "github.com/dominicf2001/comfychan/web/views/shared" +) templ NewThreadForm() { } -templ Comfy() { +templ Board(board database.Board) { @shared.Layout() {

- "/comfy/" - Comfy + { fmt.Sprintf("/%s/ - %s", board.Slug, board.Name) }

-

Be comfy fren

+

{ board.Tag }

diff --git a/web/views/boards/comfy_templ.go b/web/views/board_templ.go similarity index 70% rename from web/views/boards/comfy_templ.go rename to web/views/board_templ.go index 333b32a..a4bc355 100644 --- a/web/views/boards/comfy_templ.go +++ b/web/views/board_templ.go @@ -1,14 +1,18 @@ // Code generated by templ - DO NOT EDIT. // templ: version: v0.3.857 -package boards +package views //lint:file-ignore SA4006 This context is only used if a nested component is present. import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -import "github.com/dominicf2001/comfychan/web/views/shared" +import ( + "fmt" + database "github.com/dominicf2001/comfychan/internal/database" + "github.com/dominicf2001/comfychan/web/views/shared" +) func NewThreadForm() templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { @@ -39,7 +43,7 @@ func NewThreadForm() templ.Component { }) } -func Comfy() templ.Component { +func Board(board database.Board) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -72,7 +76,33 @@ func Comfy() templ.Component { }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "

\"/comfy/\" - Comfy

Be comfy fren

") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("/%s/ - %s", board.Slug, board.Name)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/views/board.templ`, Line: 33, Col: 54} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(board.Tag) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/views/board.templ`, Line: 35, Col: 17} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -80,7 +110,7 @@ func Comfy() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/web/views/shared/layout.templ b/web/views/shared/layout.templ index 82da626..bad6c89 100644 --- a/web/views/shared/layout.templ +++ b/web/views/shared/layout.templ @@ -19,9 +19,8 @@ templ Layout() { [ - comfy - / - r9k + comfy / + r9k ]
diff --git a/web/views/shared/layout_templ.go b/web/views/shared/layout_templ.go index 5c29408..404a81a 100644 --- a/web/views/shared/layout_templ.go +++ b/web/views/shared/layout_templ.go @@ -29,7 +29,7 @@ func Layout() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "Comfychan
[ index ] [ comfy / r9k ]
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "Comfychan
[ index ] [ comfy / r9k ]
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }