|
|
@ -2,12 +2,17 @@ package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
|
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
"io/fs" |
|
|
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
"path/filepath" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2" |
|
|
|
"github.com/gofiber/fiber/v2" |
|
|
|
_ "github.com/mattn/go-sqlite3" |
|
|
|
_ "github.com/mattn/go-sqlite3" |
|
|
|
sq "github.com/Masterminds/squirrel" |
|
|
|
sq "github.com/Masterminds/squirrel" |
|
|
|
"github.com/gofiber/fiber/v2/middleware/session" |
|
|
|
"github.com/gofiber/fiber/v2/middleware/session" |
|
|
|
|
|
|
|
"github.com/gofiber/template/html/v2" |
|
|
|
|
|
|
|
|
|
|
|
"zedshaw.games/webapp/data" |
|
|
|
"zedshaw.games/webapp/data" |
|
|
|
) |
|
|
|
) |
|
|
@ -110,24 +115,56 @@ func PostApiLink(c *fiber.Ctx) error { |
|
|
|
return IfErrNil(err, c) |
|
|
|
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) { |
|
|
|
func Setup(app *fiber.App) { |
|
|
|
STORE = session.New() |
|
|
|
STORE = session.New() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RenderPages("./pages", "./public") |
|
|
|
|
|
|
|
|
|
|
|
app.Static("/", "./public", fiber.Static{ |
|
|
|
app.Static("/", "./public", fiber.Static{ |
|
|
|
Compress: false, |
|
|
|
Compress: false, |
|
|
|
CacheDuration: 1 * time.Nanosecond, |
|
|
|
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) |
|
|
|
app.Get("/api/stream", GetApiStream) |
|
|
@ -137,8 +174,6 @@ func Setup(app *fiber.App) { |
|
|
|
app.Post("/api/login", PostApiLogin) |
|
|
|
app.Post("/api/login", PostApiLogin) |
|
|
|
app.Post("/api/link", PostApiLink) |
|
|
|
app.Post("/api/link", PostApiLink) |
|
|
|
app.Post("/api/register", PostApiRegister) |
|
|
|
app.Post("/api/register", PostApiRegister) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Shutdown() { |
|
|
|
func Shutdown() { |
|
|
|