Hot garbage but that's the idea. It will render only one page.
This commit is contained in:
parent
b9d5dbb2e9
commit
facc691343
1 changed files with 49 additions and 14 deletions
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue