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 0000000..0af4340 Binary files /dev/null and b/internal/database/comfychan.db differ 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 }