diff --git a/api/controllers.go b/api/handlers.go similarity index 67% rename from api/controllers.go rename to api/handlers.go index 8a2197f..4d696d8 100644 --- a/api/controllers.go +++ b/api/handlers.go @@ -2,17 +2,12 @@ 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" ) @@ -115,53 +110,10 @@ 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, diff --git a/main.go b/main.go index 9eb52a8..be08d33 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,10 @@ package main import ( "log" + "strings" + "io/fs" + "fmt" + "path/filepath" "os" "os/signal" "syscall" @@ -16,6 +20,56 @@ import ( "zedshaw.games/webapp/data" ) +func Fail(err error, format string, v ...any) error { + err_format := fmt.Sprintf("ERROR: %v; %s", err, format) + log.Printf(err_format, v...) + return err +} + +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 { return Fail(err, "path: %s", path); } + + dir := filepath.Dir(path) + err = os.MkdirAll(dir, 0750) + if err != nil { + return Fail(err, "making dir %s", dir); + } + + split_path := strings.Split(path, string(os.PathSeparator))[1:] + source_name := strings.Join(split_path, "/") // Render wants / even on windows + ext := filepath.Ext(source_name) + template_name, found := strings.CutSuffix(source_name, ext) + + if found && ext == ".html" { + prefixed_path := append([]string{target}, split_path...) + + target_path := filepath.Join(prefixed_path...) + + // compare time stamps and skip if not newer + + out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE, 0644) + if err != nil { return Fail(err, "writing file %s", target_path) } + + err = engine.Render(out, template_name, fiber.Map{"Title": "Hello"}) + if err != nil { return Fail(err, "failed to render %s", path) } + + log.Printf("RENDER: %s -> %s", template_name, target_path) + out.Close() + } + } + + return nil + }) + + if err != nil { log.Fatalf("can't walk content") } +} + func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) @@ -28,6 +82,8 @@ func main() { StrictRouting: true, }) + RenderPages("./pages", "./public") + app.Use(logger.New()) app.Use(recov.New()) diff --git a/pages/alpine_test.html b/pages/alpine_test.html new file mode 100644 index 0000000..a6a81d8 --- /dev/null +++ b/pages/alpine_test.html @@ -0,0 +1,9 @@ + +
+ + + + + + + diff --git a/pages/base.html b/pages/base.html new file mode 100644 index 0000000..db8b9e6 --- /dev/null +++ b/pages/base.html @@ -0,0 +1,25 @@ + + + + + + + +You have an error.
+Description
+Polaroid retro pork belly yes plz bitters, viral chicharrones typewriter chartreuse vice Brooklyn. Adaptogen pour-over vibecession viral. Tote bag tonx DIY microdosing. Pickled selvage bespoke small batch, blue bottle twee tacos jean shorts before they sold out chicharrones solarpunk. Hoodie taiyaki poutine jianbing chambray.
+Polaroid retro pork belly yes plz bitters, viral chicharrones typewriter chartreuse vice Brooklyn. Adaptogen pour-over vibecession viral. Tote bag tonx DIY microdosing. Pickled selvage bespoke small batch, blue bottle twee tacos jean shorts before they sold out chicharrones solarpunk. Hoodie taiyaki poutine jianbing chambray.
+Polaroid retro pork belly yes plz bitters, viral chicharrones typewriter chartreuse vice Brooklyn. Adaptogen pour-over vibecession viral. Tote bag tonx DIY microdosing. Pickled selvage bespoke small batch, blue bottle twee tacos jean shorts before they sold out chicharrones solarpunk. Hoodie taiyaki poutine jianbing chambray.
+Polaroid retro pork belly yes plz bitters, viral chicharrones typewriter chartreuse vice Brooklyn. Adaptogen pour-over vibecession viral. Tote bag tonx DIY microdosing. Pickled selvage bespoke small batch, blue bottle twee tacos jean shorts before they sold out chicharrones solarpunk. Hoodie taiyaki poutine jianbing chambray.
+ +More fun than a barrel full of monkeys with syphilus. +
+Polaroid retro pork belly yes plz bitters, viral chicharrones typewriter chartreuse vice Brooklyn. Adaptogen pour-over vibecession viral. Tote bag tonx DIY microdosing. Pickled selvage bespoke small batch, blue bottle twee tacos jean shorts before they sold out chicharrones solarpunk. Hoodie taiyaki poutine jianbing chambray.
+Polaroid retro pork belly yes plz bitters, viral chicharrones typewriter chartreuse vice Brooklyn. Adaptogen pour-over vibecession viral. Tote bag tonx DIY microdosing. Pickled selvage bespoke small batch, blue bottle twee tacos jean shorts before they sold out chicharrones solarpunk. Hoodie taiyaki poutine jianbing chambray.
+ +