From 0802dc3a8835bc19cf994f9eb2b462df604d4262 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Thu, 4 Sep 2025 00:51:05 +0700 Subject: feat: reroute general page derivations to general page route feat: always have page body in square aspect ratio feat: change to mobile view on 1.1/1 aspect ratio (instead of 0.8/1) feat: dynamically change title with any new page feat: cache general page template --- internal/router/api-v1-general-page-body.go | 1 + internal/router/api-v1-general-page.go | 52 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 internal/router/api-v1-general-page.go (limited to 'internal') diff --git a/internal/router/api-v1-general-page-body.go b/internal/router/api-v1-general-page-body.go index 2329c4a..cdb558a 100644 --- a/internal/router/api-v1-general-page-body.go +++ b/internal/router/api-v1-general-page-body.go @@ -108,6 +108,7 @@ func Api_V1_GeneralPage_Body(l map[string]*locale.LocaleConfig, langs []string, values["Tags"] = tagsArray values["QuerySort"] = querySort values["QueryTags"] = strings.Join(queryTags, ",") + values["Title"] = l[lang].BlogSearch.Header additionalTemplates = append(additionalTemplates, "views/pages/blog-catalogue.html") } else if len(pathParts) == 3 && pathParts[1] == "blog" { diff --git a/internal/router/api-v1-general-page.go b/internal/router/api-v1-general-page.go new file mode 100644 index 0000000..8e1387b --- /dev/null +++ b/internal/router/api-v1-general-page.go @@ -0,0 +1,52 @@ +package router + +import ( + "fmt" + "log/slog" + "slices" + "strings" + + "github.com/SayaAndy/saya-today-web/locale" + "github.com/gofiber/fiber/v2" +) + +func init() { + tm.Add("general-page", "views/layouts/general-page.html") +} + +func Api_V1_GeneralPage(l map[string]*locale.LocaleConfig, langs []string) func(c *fiber.Ctx) error { + return func(c *fiber.Ctx) error { + c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8) + path := c.Path() + + pathParts := strings.Split(strings.Trim(path, "/"), "/") + if len(pathParts) < 2 { + return c.Status(fiber.ErrBadRequest.Code).SendString("url path is invalid: expect format '/{lang}/...'") + } + + lang := pathParts[0] + if !slices.Contains(langs, lang) { + return c.Status(fiber.ErrNotFound.Code).SendString(fmt.Sprintf("server does not support '%s' language... yet??", lang)) + } + + cacheKey := "general-page." + lang + if val, ok := PCache.Get(cacheKey); val != nil && ok { + c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8) + return c.Status(fiber.StatusOK).Type("html").Send(val) + } + + content, err := tm.Render("general-page", fiber.Map{ + "L": l[lang], + "Lang": lang, + "QueryString": string(c.Request().URI().QueryString()), + }) + if err != nil { + slog.Warn("failed to generate div", slog.String("path", path), slog.String("error", err.Error())) + return c.Status(fiber.ErrInternalServerError.Code).SendString("failed to generate div") + } + + go PCache.Set(cacheKey, content, int64(len(content))) + c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8) + return c.Status(fiber.StatusOK).Type("html").Send(content) + } +} -- cgit v1.3.1+13