summaryrefslogtreecommitdiffci
path: root/internal/router/handlers/api-v1-tz-get.go
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <saya.andy@posteo.com> 2025-10-28 23:04:53 +0700
committerGravatar SayaAndy <saya.andy@posteo.com> 2025-10-28 23:04:53 +0700
commit1862f232eb70105340a91e74a5630c233a411bca (patch)
tree464c18fed0c21b63b65425f48a3a790229461058 /internal/router/handlers/api-v1-tz-get.go
parent629e275e5270ed146f9cd6dba25383cf80022909 (diff)
downloadweb-1862f232eb70105340a91e74a5630c233a411bca.tar.gz
web-1862f232eb70105340a91e74a5630c233a411bca.zip
refactor: make structurized implementation of routes + separate identity of router
feat: update go to 1.25 feat: update all dependencies debug: display routes when debug logs turned on
Diffstat (limited to 'internal/router/handlers/api-v1-tz-get.go')
-rw-r--r--internal/router/handlers/api-v1-tz-get.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/internal/router/handlers/api-v1-tz-get.go b/internal/router/handlers/api-v1-tz-get.go
new file mode 100644
index 0000000..8b08113
--- /dev/null
+++ b/internal/router/handlers/api-v1-tz-get.go
@@ -0,0 +1,65 @@
+package handlers
+
+import (
+ "fmt"
+ "log/slog"
+ "time"
+
+ "github.com/SayaAndy/saya-today-web/internal/router"
+ "github.com/gofiber/fiber/v2"
+)
+
+type GetTimezoneHandler struct {
+ router.BasicHandler
+}
+
+func init() {
+ router.Routes = append(router.Routes, &GetTimezoneHandler{})
+}
+
+func (r *GetTimezoneHandler) Filter() (method string, path string) {
+ return "GET", "/api/v1/tz"
+}
+
+func (r *GetTimezoneHandler) IsTemplated() bool {
+ return false
+}
+
+func (r *GetTimezoneHandler) TemplatesToInject() []string {
+ return []string{}
+}
+
+func (r *GetTimezoneHandler) ToCache() router.CacheSetting {
+ return router.Disabled
+}
+
+func (r *GetTimezoneHandler) ToValidateLang() router.LangSetting {
+ return router.NotRequired
+}
+
+func (r *GetTimezoneHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
+ timestampString := c.Query("timestamp")
+ tz := c.Query("tz")
+
+ loc, err := time.LoadLocation(tz)
+ if err != nil {
+ loc = time.UTC
+ slog.Warn("unable to parse a client timezone, defaulting to UTC", slog.String("error", err.Error()), slog.String("tz", tz))
+ }
+
+ var format string
+ var timestamp time.Time
+ for _, format = range []string{"2006-01-02 15:04:05 -07:00", time.RFC3339} {
+ if timestamp, err = time.Parse(format, timestampString); err == nil {
+ break
+ }
+ }
+
+ if err != nil {
+ return fiber.StatusBadRequest, fmt.Errorf("invalid timestamp format for '%s'", timestampString)
+ }
+
+ templateMap["Output"] = []byte(timestamp.In(loc).Format("2006-01-02 15:04:05 -07:00"))
+
+ return fiber.StatusOK, nil
+}