summaryrefslogtreecommitdiff
path: root/internal/router/api-v1-general-page-bottom-embeds.go
diff options
from:
to:
context:
space:
mode:
Diffstat (limited to 'internal/router/api-v1-general-page-bottom-embeds.go')
-rw-r--r--internal/router/api-v1-general-page-bottom-embeds.go25
1 files changed, 17 insertions, 8 deletions
diff --git a/internal/router/api-v1-general-page-bottom-embeds.go b/internal/router/api-v1-general-page-bottom-embeds.go
index c4d9543..0a2030f 100644
--- a/internal/router/api-v1-general-page-bottom-embeds.go
+++ b/internal/router/api-v1-general-page-bottom-embeds.go
@@ -4,10 +4,10 @@ import (
"fmt"
"log/slog"
"net/url"
- "slices"
"strings"
"time"
+ "github.com/SayaAndy/saya-today-web/config"
"github.com/SayaAndy/saya-today-web/internal/b2"
"github.com/SayaAndy/saya-today-web/locale"
"github.com/gofiber/fiber/v2"
@@ -17,7 +17,7 @@ func init() {
tm.Add("general-page-bottom-embeds", "views/partials/general-page-bottom-embeds.html")
}
-func Api_V1_GeneralPage_BottomEmbeds(l map[string]*locale.LocaleConfig, langs []string, b2Client *b2.B2Client) func(c *fiber.Ctx) error {
+func Api_V1_GeneralPage_BottomEmbeds(l map[string]*locale.LocaleConfig, langs []config.AvailableLanguageConfig, b2Client *b2.B2Client) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
@@ -37,19 +37,26 @@ func Api_V1_GeneralPage_BottomEmbeds(l map[string]*locale.LocaleConfig, langs []
return c.Status(fiber.StatusOK).Type("html").Send(val)
}
+ lang := ""
pathParts := strings.Split(strings.Trim(path, "/"), "/")
- if len(pathParts) == 0 {
- return c.Status(fiber.ErrBadRequest.Code).SendString("'Referer' header is invalid: expect format '/{lang}/...'")
+ if len(pathParts) == 1 && pathParts[0] == "" {
+ pathParts = []string{}
}
-
- 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))
+ if len(pathParts) > 0 {
+ lang = pathParts[0]
+ for _, availableLang := range langs {
+ if availableLang.Name == lang {
+ goto langIsAvailable
+ }
+ }
+ return c.Status(fiber.ErrBadRequest.Code).SendString("'Referer' header is invalid: expect format '/{lang}/...'")
}
+ langIsAvailable:
values := fiber.Map{
"L": l[lang],
"Lang": lang,
+ "Path": strings.Trim(path, "/"),
"QueryString": string(c.Request().URI().QueryString()),
}
var additionalTemplates []string
@@ -60,6 +67,8 @@ func Api_V1_GeneralPage_BottomEmbeds(l map[string]*locale.LocaleConfig, langs []
additionalTemplates = append(additionalTemplates, "views/pages/blog-page.html")
} else if len(pathParts) == 1 {
additionalTemplates = append(additionalTemplates, "views/pages/home-page.html")
+ } else if len(pathParts) == 0 {
+ additionalTemplates = append(additionalTemplates, "views/pages/language-pick.html")
}
content, err := tm.Render("general-page-bottom-embeds", values, additionalTemplates...)