Briefansicht

This commit is contained in:
Simon Martens
2025-03-27 21:34:29 +01:00
parent a224d31c47
commit 316e8e3f58
8 changed files with 122 additions and 52 deletions

27
controllers/brief.go Normal file
View File

@@ -0,0 +1,27 @@
package controllers
import (
"strconv"
"github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels"
"github.com/gofiber/fiber/v2"
)
func GetLetter(c *fiber.Ctx) error {
lib := xmlmodels.Get()
l := c.Params(LETTER_PARAM)
letter, err := strconv.Atoi(l)
if err != nil {
return c.SendStatus(fiber.StatusNotFound)
}
meta := lib.Metas.Item(letter)
if meta == nil {
return c.SendStatus(fiber.StatusNotFound)
}
text := lib.Letters.Item(letter)
tradition := lib.Traditions.Item(letter)
return c.Render("/brief/", map[string]any{"meta": meta, "text": text, "tradition": tradition})
}

View File

@@ -1,16 +1,27 @@
package controllers
import (
"strconv"
"github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels"
"github.com/gofiber/fiber/v2"
)
const DEFAULT_YEAR = 1765
func GetIndex(fiber *fiber.Ctx) error {
func GetIndex(c *fiber.Ctx) error {
return c.Redirect("/" + strconv.Itoa(DEFAULT_YEAR))
}
func GetIndexYear(c *fiber.Ctx) error {
lib := xmlmodels.Get()
// Years
years, yearmap := lib.Years()
return fiber.Render("/", map[string]any{"years": years, "yearmap": yearmap})
y := c.Params(YEAR_PARAM)
year, err := strconv.Atoi(y)
if _, ok := yearmap[year]; (err != nil || !ok) && y != "all" {
return c.SendStatus(fiber.StatusNotFound)
}
return c.Render("/", map[string]any{"years": years, "yearmap": yearmap, "year": year, "all": y == "all"})
}

View File

@@ -10,21 +10,25 @@ import (
)
const ASSETS_URL = "/assets"
const WBHOOK_URL = "/webhook"
const INDEX_URL = "/"
const YEAR_PARAM = "year"
const LETTER_PARAM = "letter"
var INDEX_YEAR_URL = "/:" + YEAR_PARAM
var LETTER_URL = "/brief/:" + LETTER_PARAM
func Register(server server.Server, cfg config.Config) {
server.Server.Use(ASSETS_URL, compress.New(compress.Config{
Level: compress.LevelBestSpeed,
}))
server.Server.Use(ASSETS_URL, middleware.StaticHandler(&views.StaticFS))
server.Server.Get("/", GetIndex)
server.Server.Get(INDEX_URL, GetIndex)
server.Server.Get(INDEX_YEAR_URL, GetIndexYear)
server.Server.Get(LETTER_URL, GetLetter)
// INFO: we map the webhook when a secret was provided
if cfg.WebHookSecret != "" {
whurl := WBHOOK_URL
if cfg.WebHookEndpoint != "" {
whurl = cfg.WebHookEndpoint
}
server.Server.Post(whurl, PostWebhook(cfg))
server.Server.Post(cfg.WebHookEndpoint, PostWebhook(cfg))
}
}