summaryrefslogtreecommitdiff
path: root/internal/handlers.go
blob: c151904a531a97bbd50cc3e7181d72f15de17104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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")
	}

	content, err := s.tmpl.GetContent(page, PageSections[page][0])
	if err != nil {
		log.Printf("Error rendering content for %s: %v", page, 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, "page.html", data)
}

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)
}