Came up with the pages/ directory that's templates which are rendered into public for the static pages.

This commit is contained in:
Zed A. Shaw 2025-07-04 11:31:15 -04:00
parent facc691343
commit f476605ecf
19 changed files with 795 additions and 61 deletions

View file

@ -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,