package common import ( "strings" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/session" "io/fs" "path/filepath" "fmt" ) var STORE *session.Store func AddAuthedPage(app *fiber.App, must_admin bool, url string, view string) { handler := func(c *fiber.Ctx) error { _, err := AuthCheck(c, must_admin) if err != nil { return c.Redirect("/") } return c.Render(view, c.AllParams()) } app.Get(url, handler) } func AddPage(app *fiber.App, url string, view string) { handler := func(c *fiber.Ctx) error { return c.Render(view, c.AllParams()) } app.Get(url, handler) } func Page(path string) (func(c *fiber.Ctx) error) { page_id := strings.ReplaceAll(path, "/", "-") + "-page" return func (c *fiber.Ctx) error { return c.Render(path, fiber.Map{"PageId": page_id}) } } func SplitRouteTemplate(path string) (string, string) { path = filepath.ToSlash(filepath.Clean(path)) path, found := strings.CutPrefix(path, "views/") if !found { fmt.Println("no views/ prefix?", path) } ext := filepath.Ext(path) source_name, _ := strings.CutSuffix(path, ext) split_path := strings.Split(source_name, "/") var route string page := strings.Join(split_path, "/") last := split_path[len(split_path) - 1] if last == "index" { // it's an index.html so route doesn't include it // WARN: page already has the leading / route = fmt.Sprintf("/%s/", filepath.Dir(page)) } else { route = fmt.Sprintf("/%s", page) } return route, page } func ConfigViews(app *fiber.App, path string) error { route_map := make(map[string]string, 10) for _, route := range app.GetRoutes() { if route.Method == "GET" { route_map[route.Path] = route.Method } } err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if !d.IsDir() && strings.HasSuffix(path, "html") { route, page := SplitRouteTemplate(path) _, exists := route_map[route] if exists { fmt.Println("ROUTE ", route, "Already exists, skipping", page) } else { fmt.Println("ROUTE ", route, "does not conflict page", page) AddPage(app, route, page) // update the route map to avoid dupes route_map[route] = "GET" } } return nil }) return err }