diff options
Diffstat (limited to 'internal/handlers.go')
| -rw-r--r-- | internal/handlers.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/internal/handlers.go b/internal/handlers.go new file mode 100644 index 0000000..5d66be8 --- /dev/null +++ b/internal/handlers.go @@ -0,0 +1,98 @@ +package internal + +import ( + "log" + "net/http" + "slices" + + "github.com/labstack/echo/v4" +) + +func (s *Server) handleHome(c echo.Context) error { + return s.renderFullPage(c, PageHome, PageSections[PageHome][0]) +} + +func (s *Server) handlePage(c echo.Context) error { + page := c.Param("page") + if !validPage(page) { + return c.String(http.StatusNotFound, "Page not found") + } + return s.renderFullPage(c, page, PageSections[page][0]) +} + +func (s *Server) handlePageSection(c echo.Context) error { + page := c.Param("page") + section := c.Param("section") + + if !validPage(page) { + return c.String(http.StatusNotFound, "Page not found") + } + + if !validSection(page, section) { + return c.String(http.StatusNotFound, "Section not found") + } + + return s.renderFullPage(c, page, section) +} + +func (s *Server) handleHTMXPage(c echo.Context) error { + page := c.Param("page") + if !validPage(page) { + return c.String(http.StatusNotFound, "Page not found") + } + return s.renderContent(c, page, PageSections[page][0]) +} + +func (s *Server) handleHTMXSection(c echo.Context) error { + page := c.Param("page") + section := c.Param("section") + + if !validPage(page) { + return c.String(http.StatusNotFound, "Page not found") + } + + if !validSection(page, section) { + return c.String(http.StatusNotFound, "Section not found") + } + + return s.renderContent(c, page, section) +} + +func (s *Server) renderFullPage(c echo.Context, page, section string) error { + content, err := s.tmpl.GetContent(page, section) + if err != nil { + log.Printf("Error rendering content for %s/%s: %v", page, section, err) + return c.String(http.StatusInternalServerError, "Error loading content") + } + + data := PageData{ + Page: page, + Pages: ValidPages, + Sections: PageSections[page], + Content: content, + } + + return c.Render(http.StatusOK, "layout.html", data) +} + +func (s *Server) renderContent(c echo.Context, page, section string) error { + content, err := s.tmpl.GetContent(page, section) + if err != nil { + log.Printf("Error rendering content for %s/%s: %v", page, section, err) + return c.String(http.StatusInternalServerError, "Error loading content") + } + + return c.HTML(http.StatusOK, string(content)) +} + +func validPage(page string) bool { + return slices.Contains(ValidPages, page) +} + +func validSection(page, section string) bool { + sections, exists := PageSections[page] + if !exists { + return false + } + return slices.Contains(sections, section) +} |