From facc691343583348ec4cd38251c41472214f84b5 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 4 Jul 2025 00:43:19 -0400 Subject: [PATCH] Hot garbage but that's the idea. It will render only one page. --- api/controllers.go | 63 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/api/controllers.go b/api/controllers.go index 837522f..8a2197f 100644 --- a/api/controllers.go +++ b/api/controllers.go @@ -2,12 +2,17 @@ package api import ( "log" + "strings" "time" + "io/fs" + "os" + "path/filepath" "github.com/gofiber/fiber/v2" _ "github.com/mattn/go-sqlite3" sq "github.com/Masterminds/squirrel" "github.com/gofiber/fiber/v2/middleware/session" + "github.com/gofiber/template/html/v2" "zedshaw.games/webapp/data" ) @@ -110,24 +115,56 @@ func PostApiLink(c *fiber.Ctx) error { return IfErrNil(err, c) } +func RenderPages(pages_path string, target string) { + + engine := html.New(pages_path, ".html") + engine.Load() + + err := filepath.WalkDir(pages_path, + func(path string, d fs.DirEntry, err error) error { + if !d.IsDir() { + if err != nil { log.Printf("ERROR: %v", err) } + + dir := filepath.Dir(path) + err = os.MkdirAll(dir, 0750) + if err != nil { log.Fatalf("ERROR making dir %s, %v", dir, err) } + + split_path := strings.Split(path, string(os.PathSeparator))[1:] + source_name := filepath.Join(split_path...) + ext := filepath.Ext(source_name) + name, found := strings.CutSuffix(source_name, ext) + + if found { + log.Printf("name=%s, ext=%s, dir=%s, found=%v", name, ext, dir, found) + + prefixed_path := append([]string{target}, split_path...) + + target_path := filepath.Join(prefixed_path...) + log.Printf("target_path: %s", target_path) + + out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE, 0644) + if err != nil { log.Fatalf("ERROR writing file %s", target_path) } + + err = engine.Render(out, "game/1/turings-tarpit/index", fiber.Map{"Title": "Hello"}) + if err != nil { log.Fatalf("failed to render %s, error=%v", path, err) } + out.Close() + } + } + + return nil + }) + + if err != nil { log.Fatalf("can't walk content") } +} + func Setup(app *fiber.App) { STORE = session.New() + RenderPages("./pages", "./public") + app.Static("/", "./public", fiber.Static{ Compress: false, CacheDuration: 1 * time.Nanosecond, - Next: func(c *fiber.Ctx) bool { - // true means skip, so I can check if the url matches - // a view, and if it does then skip which will leave - // it to the view handler next - return false - }, - }) - - app.Get("/v/:name/", func (c *fiber.Ctx) error { - return c.Render(c.Params("name"), fiber.Map{ - "Title": "Hello, World!", - }) }) app.Get("/api/stream", GetApiStream) @@ -137,8 +174,6 @@ func Setup(app *fiber.App) { app.Post("/api/login", PostApiLogin) app.Post("/api/link", PostApiLink) app.Post("/api/register", PostApiRegister) - - } func Shutdown() {