-
Notifications
You must be signed in to change notification settings - Fork 0
/
adminroutes.go
63 lines (51 loc) · 1.53 KB
/
adminroutes.go
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
package handler
import (
"fmt"
"net/http"
"github.com/bnema/gordon/internal/server"
"github.com/bnema/gordon/internal/templating/render"
"github.com/labstack/echo/v4"
)
var (
mainPath = "html/admin"
fragmentsPath = "html/fragments"
)
// GetLocalizedData returns the data for the localization
func GetLocalizedData(c echo.Context, a *server.App) (map[string]interface{}, error) {
lang := c.Get("LangKey")
if lang == nil {
return nil, fmt.Errorf("LangKey not found in context")
}
yamlData, err := render.GetLocalization(lang.(string), a)
if err != nil {
return nil, fmt.Errorf("failed to get localization: %w", err)
}
return map[string]interface{}{
"Lang": yamlData,
"LogoutURL": a.Config.Admin.Path + "/logout",
}, nil
}
// renderAdminPage is a generalized function to render admin pages
func renderAdminPage(c echo.Context, a *server.App, templateName string) error {
data, err := GetLocalizedData(c, a)
if err != nil {
return err
}
rendererData, err := render.GetHTMLRenderer(mainPath, templateName, a.TemplateFS, a, fragmentsPath)
if err != nil {
return err
}
renderedHTML, err := rendererData.Render(data, a)
if err != nil {
return err
}
return c.HTML(http.StatusOK, renderedHTML)
}
// AdminRoute is the route for the admin panel
func AdminRoute(c echo.Context, a *server.App) error {
return renderAdminPage(c, a, "index.gohtml")
}
// AdminManagerRoute is the route of the manager page
func AdminManagerRoute(c echo.Context, a *server.App) error {
return renderAdminPage(c, a, "manager.gohtml")
}