From 859e3ad0e3c5ec501f0b44e0ba3782dc3905af2e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 13 Jul 2025 12:28:03 -0400 Subject: [PATCH] Initial setup of an admin page that dynamically reflects the DB to create the CRUD stuff. --- Makefile | 4 ++++ admin/handlers.go | 7 +++++-- data/models.go | 10 ++++++++++ main.go | 4 +++- tests/admin_test.go | 23 +++++++++++++++++++++++ views/admin/index.html | 1 + 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/admin_test.go create mode 100644 views/admin/index.html diff --git a/Makefile b/Makefile index 707d571..7c8b664 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,10 @@ test: site go test zedshaw.games/webapp/tests -c -o runtests$(GO_IS_STUPID_EXE) ./runtests$(GO_IS_STUPID_EXE) +test_only: + go test zedshaw.games/webapp/tests -c -o runtests$(GO_IS_STUPID_EXE) + ./runtests$(GO_IS_STUPID_EXE) -test.run TestAdminIndexPage + migrate_up: go tool goose sqlite3 db.sqlite3 -dir migrations up diff --git a/admin/handlers.go b/admin/handlers.go index 1629e2f..2898b64 100644 --- a/admin/handlers.go +++ b/admin/handlers.go @@ -1,10 +1,13 @@ package admin +import ( + "github.com/gofiber/fiber/v2" +) func GetPageIndex(c *fiber.Ctx) error { - c.Render("admin/index", fiber.Map{}) + return c.Render("admin/index", fiber.Map{}) } func Setup(app *fiber.App) { - app.Get("/admin/", GetIndex); + app.Get("/admin/", GetPageIndex); } diff --git a/data/models.go b/data/models.go index b5e962f..436227d 100644 --- a/data/models.go +++ b/data/models.go @@ -1,5 +1,7 @@ package data +import "reflect" + type Login struct { Username string `db:"username" validate:"required,max=30"` Password string `db:"password" validate:"required,max=128"` @@ -23,3 +25,11 @@ type Stream struct { Title string `db:"title" json:"title"` Description string `db:"description" json:"description"` } + +func Models() map[string]reflect.Type { + return map[string]reflect.Type{ + "stream": reflect.TypeFor[Stream](), + "stream_link": reflect.TypeFor[Link](), + "user": reflect.TypeFor[User](), + } +} diff --git a/main.go b/main.go index 1481d58..af232cb 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "zedshaw.games/webapp/api" "zedshaw.games/webapp/data" "zedshaw.games/webapp/config" + "zedshaw.games/webapp/admin" ) func main() { @@ -36,8 +37,9 @@ func main() { app.Use(logger.New()) app.Use(recov.New()) - api.Setup(app) data.Setup(config.Settings.Database.Driver, config.Settings.Database.Url) + api.Setup(app) + admin.Setup(app) // this sets up graceful shutdown go func() { diff --git a/tests/admin_test.go b/tests/admin_test.go new file mode 100644 index 0000000..e2af8ba --- /dev/null +++ b/tests/admin_test.go @@ -0,0 +1,23 @@ +package tests + +import ( + "testing" + "fmt" + "reflect" + "zedshaw.games/webapp/data" + // _ "github.com/mattn/go-sqlite3" + // "github.com/jmoiron/sqlx" +) + +func TestAdminIndexPage(t *testing.T) { + models := data.Models() + + for table, model := range models { + fmt.Printf("\n------\ntable=%s; model=%s\n", table, model.Name()) + fields := reflect.VisibleFields(model) + + for _, field := range fields { + fmt.Println("\t", field.Name, field.Type, field.Tag) + } + } +} diff --git a/views/admin/index.html b/views/admin/index.html new file mode 100644 index 0000000..5490645 --- /dev/null +++ b/views/admin/index.html @@ -0,0 +1 @@ +

Admin!