diff options
| author | Mikkel Thestrup <mikkel_thestrup@mithe.dk> | 2025-12-06 19:29:47 +0100 |
|---|---|---|
| committer | Mikkel Thestrup <mikkel_thestrup@mithe.dk> | 2025-12-06 19:29:47 +0100 |
| commit | 21e9956d25720998c3f59e052dfa84fb13d513bf (patch) | |
| tree | 37ce4c877f46248e1ccf3cae495e88476a504561 /internal/handlers.go | |
| parent | b668f82b3bb834265c842bff96c252a513afa647 (diff) | |
| download | web-portfolio-21e9956d25720998c3f59e052dfa84fb13d513bf.tar.gz web-portfolio-21e9956d25720998c3f59e052dfa84fb13d513bf.zip | |
Compartmentalize the code
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) +} |