From 2d727cc1eeb926f5b5ee578a0ee54269e0390a96 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sat, 2 May 2026 12:58:40 +0700 Subject: refactor: parametrize static & photo storage url refactor: parametrize allow origins --- config/config.go | 23 ++++++++++++++++ config/config.local.yaml | 22 +++++++++++++++ config/config.prod.yaml | 22 +++++++++++++++ config/config.stage.yaml | 22 +++++++++++++++ internal/glightbox/extension.go | 11 +++++--- internal/glightbox/html_renderer.go | 42 ++++++++++++++++++++--------- internal/router/handlers/lang-blog-title.go | 2 +- internal/router/handlers/lang.go | 10 +++++-- internal/router/router.go | 42 ++++++++++++++++++----------- views/layouts/general-page.html | 16 +++++------ views/messages/new-post.html | 2 +- views/pages/global-map.html | 14 +++++----- views/pages/home-page.html | 2 +- views/partials/catalogue-blog-cards.html | 2 +- 14 files changed, 178 insertions(+), 54 deletions(-) diff --git a/config/config.go b/config/config.go index ebe0367..536a333 100644 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,9 @@ type Config struct { Mail MailConfig `json:"Mail" yaml:"mail" validate:"required"` CanonicalEndpoint string `json:"CanonicalEndpoint" yaml:"canonicalEndpoint" validate:"required"` Meta MetaConfig `json:"Meta" yaml:"meta"` + PhotoStorage PhotoStorageConfig `json:"PhotoStorage" yaml:"photoStorage"` + StaticStorage PhotoTypeConfig `json:"StaticStorage" yaml:"staticStorage"` + AllowOrigins []string `json:"AllowOrigins" yaml:"allowOrigins"` } type BlogPagesConfig struct { @@ -158,6 +161,26 @@ type MetaConfig struct { GoogleSiteVerification string `json:"GoogleSiteVerification" yaml:"googleSiteVerification"` } +type PhotoStorageConfig struct { + Full PhotoTypeConfig `json:"Full" yaml:"full" validate:"required"` + Webp PhotoTypeConfig `json:"Webp" yaml:"webp"` + Thumbnail1600p PhotoTypeConfig `json:"Thumbnail1600p" yaml:"thumbnail1600p"` + Thumbnail1200p PhotoTypeConfig `json:"Thumbnail1200p" yaml:"thumbnail1200p"` + Thumbnail800p PhotoTypeConfig `json:"Thumbnail800p" yaml:"thumbnail800p"` + Thumbnail560p PhotoTypeConfig `json:"Thumbnail560p" yaml:"thumbnail560p"` + Thumbnail320p PhotoTypeConfig `json:"Thumbnail320p" yaml:"thumbnail320p"` + HomePageGifs HomePageGifsConfig `json:"HomePageGifs" yaml:"homePageGifs"` +} + +type PhotoTypeConfig struct { + BaseUrl string `json:"BaseUrl" yaml:"baseUrl" validate:"url,required"` +} + +type HomePageGifsConfig struct { + BaseUrl string `json:"BaseUrl" yaml:"baseUrl" validate:"url,required"` + Indexes []string `json:"Indexes" yaml:"indexes"` +} + func LoadConfig(path string, config *Config) error { fileBytes, err := os.ReadFile(path) if err != nil { diff --git a/config/config.local.yaml b/config/config.local.yaml index c02dc2d..d312504 100644 --- a/config/config.local.yaml +++ b/config/config.local.yaml @@ -46,3 +46,25 @@ mail: trigger: onNewPost: "* * * * *" canonicalEndpoint: 'http://127.0.0.1:3000' +photoStorage: + full: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/full/%s + webp: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp/%s.webp + thumbnail1600p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-1600p/%s.webp + thumbnail1200p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-1200p/%s.webp + thumbnail800p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-800p/%s.webp + thumbnail560p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-560p/%s.webp + thumbnail320p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-320p/%s.webp + homePageGifs: + baseUrl: https://f003.backblazeb2.com/file/sayana-static/home-page-gifs/otter-%s.gif + indexes: ['1', '2', '3'] +staticStorage: + baseUrl: https://f003.backblazeb2.com/file/sayana-static +allowOrigins: + - https://f003.backblazeb2.com diff --git a/config/config.prod.yaml b/config/config.prod.yaml index b40ba57..2c5a159 100644 --- a/config/config.prod.yaml +++ b/config/config.prod.yaml @@ -47,3 +47,25 @@ mail: canonicalEndpoint: 'https://${FQDN}' meta: googleSiteVerification: '${GOOGLE_SITE_VERIFICATION}' +photoStorage: + full: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/full/%s + webp: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp/%s.webp + thumbnail1600p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-1600p/%s.webp + thumbnail1200p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-1200p/%s.webp + thumbnail800p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-800p/%s.webp + thumbnail560p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-560p/%s.webp + thumbnail320p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-320p/%s.webp + homePageGifs: + baseUrl: https://f003.backblazeb2.com/file/sayana-static/home-page-gifs/otter-%s.gif + indexes: ['1', '2', '3'] +staticStorage: + baseUrl: https://f003.backblazeb2.com/file/sayana-static +allowOrigins: + - https://f003.backblazeb2.com diff --git a/config/config.stage.yaml b/config/config.stage.yaml index efa94ae..0889c19 100644 --- a/config/config.stage.yaml +++ b/config/config.stage.yaml @@ -45,3 +45,25 @@ mail: trigger: onNewPost: "0/3 * * * *" canonicalEndpoint: 'https://${FQDN}' +photoStorage: + full: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/full/%s + webp: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp/%s.webp + thumbnail1600p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-1600p/%s.webp + thumbnail1200p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-1200p/%s.webp + thumbnail800p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-800p/%s.webp + thumbnail560p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-560p/%s.webp + thumbnail320p: + baseUrl: https://f003.backblazeb2.com/file/sayana-photos/webp-320p/%s.webp + homePageGifs: + baseUrl: https://f003.backblazeb2.com/file/sayana-static/home-page-gifs/otter-%s.gif + indexes: ['1', '2', '3'] +staticStorage: + baseUrl: https://f003.backblazeb2.com/file/sayana-static +allowOrigins: + - https://f003.backblazeb2.com diff --git a/internal/glightbox/extension.go b/internal/glightbox/extension.go index 7fd17b1..e9a9f3e 100644 --- a/internal/glightbox/extension.go +++ b/internal/glightbox/extension.go @@ -1,6 +1,7 @@ package glightbox import ( + "github.com/SayaAndy/saya-today-web/config" "github.com/yuin/goldmark" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer" @@ -8,10 +9,12 @@ import ( ) // Extension that combines parser and renderer -type GLightboxExtension struct{} +type GLightboxExtension struct { + photoStorage config.PhotoStorageConfig +} -func NewGLightboxExtension() goldmark.Extender { - return &GLightboxExtension{} +func NewGLightboxExtension(photoStorage config.PhotoStorageConfig) goldmark.Extender { + return &GLightboxExtension{photoStorage} } func (e *GLightboxExtension) Extend(m goldmark.Markdown) { @@ -22,7 +25,7 @@ func (e *GLightboxExtension) Extend(m goldmark.Markdown) { ) m.Renderer().AddOptions( renderer.WithNodeRenderers( - util.Prioritized(NewGLightboxHTMLRenderer(), 500), + util.Prioritized(NewGLightboxHTMLRenderer(e.photoStorage), 500), ), ) } diff --git a/internal/glightbox/html_renderer.go b/internal/glightbox/html_renderer.go index f71a8f2..b2cd855 100644 --- a/internal/glightbox/html_renderer.go +++ b/internal/glightbox/html_renderer.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/SayaAndy/saya-today-web/config" "github.com/SayaAndy/saya-today-web/internal/tailwind" "github.com/yuin/goldmark" "github.com/yuin/goldmark/ast" @@ -21,9 +22,10 @@ type GLightboxHTMLRenderer struct { html.Config md goldmark.Markdown anchorMatchRe *regexp.Regexp + photoStorage config.PhotoStorageConfig } -func NewGLightboxHTMLRenderer(opts ...html.Option) renderer.NodeRenderer { +func NewGLightboxHTMLRenderer(photoStorage config.PhotoStorageConfig, opts ...html.Option) renderer.NodeRenderer { r := &GLightboxHTMLRenderer{ Config: html.NewConfig(), md: goldmark.New( @@ -44,11 +46,12 @@ func NewGLightboxHTMLRenderer(opts ...html.Option) renderer.NodeRenderer { ), ), ), + anchorMatchRe: regexp.MustCompile(`(?s)<\s*a(\s+[^<]*)(href\s*=\s*["'].*?["'])\s*([^<]*)>(.*?)<\s*\/\s*a\s*>`), + photoStorage: photoStorage, } for _, opt := range opts { opt.SetHTMLOption(&r.Config) } - r.anchorMatchRe = regexp.MustCompile(`(?s)<\s*a(\s+[^<]*)(href\s*=\s*["'].*?["'])\s*([^<]*)>(.*?)<\s*\/\s*a\s*>`) return r } @@ -111,19 +114,32 @@ func (r *GLightboxHTMLRenderer) renderGLightbox(w util.BufWriter, source []byte, anchorlessCaptionHTML := r.anchorMatchRe.ReplaceAll(captionHTML, []byte("$4")) elements = append(elements, fmt.Sprintf(` - + - - - - - - + + + + + + -

%s

- %d -
`, fullImageUrl, strings.Join(tagClassList, " "), galleryID, dayDate.Format("2006-01-02 15:04:05 -07:00"), dataDescriptionAttribute, imageUrlWithoutExt, imageUrlWithoutExt, imageUrlWithoutExt, imageUrlWithoutExt, imageUrlWithoutExt, imageUrlWithoutExt, anchorlessCaptionHTML, i+1)) +

%[5]s

+ %[6]d + `, + strings.Join(tagClassList, " "), + galleryID, + dayDate.Format("2006-01-02 15:04:05 -07:00"), + dataDescriptionAttribute, + anchorlessCaptionHTML, + i+1, + fmt.Sprintf(r.photoStorage.Full.BaseUrl, fullImageUrl), + fmt.Sprintf(r.photoStorage.Thumbnail1600p.BaseUrl, imageUrlWithoutExt), + fmt.Sprintf(r.photoStorage.Thumbnail1200p.BaseUrl, imageUrlWithoutExt), + fmt.Sprintf(r.photoStorage.Thumbnail800p.BaseUrl, imageUrlWithoutExt), + fmt.Sprintf(r.photoStorage.Thumbnail560p.BaseUrl, imageUrlWithoutExt), + fmt.Sprintf(r.photoStorage.Thumbnail320p.BaseUrl, imageUrlWithoutExt), + )) if glightboxDescId != "" { elements = append(elements, fmt.Sprintf(` diff --git a/internal/router/handlers/lang-blog-title.go b/internal/router/handlers/lang-blog-title.go index f1be0a5..49744c1 100644 --- a/internal/router/handlers/lang-blog-title.go +++ b/internal/router/handlers/lang-blog-title.go @@ -74,7 +74,7 @@ func (r *BlogPageHandler) AddMeta(c *fiber.Ctx, supplements *router.Supplements, return []router.MetaField{ {Property: "og:title", Content: metadata.Title}, {Property: "og:description", Content: fmt.Sprintf("%s [%s]", metadata.ShortDescription, metadata.ActionDate)}, - {Property: "og:image", Content: fmt.Sprintf("https://f003.backblazeb2.com/file/sayana-photos/webp-320p/%s.webp", metadata.Thumbnail)}, + {Property: "og:image", Content: fmt.Sprintf(supplements.PhotoStorage.Thumbnail320p.BaseUrl, metadata.Thumbnail)}, {Property: "og:url", Content: fmt.Sprintf("%s/%s/blog/%s", templateMap["CanonicalEndpoint"], lang, c.Params("title"))}, {Property: "og:type", Content: "website"}, {Name: "twitter:card", Content: "summary_large_image"}, diff --git a/internal/router/handlers/lang.go b/internal/router/handlers/lang.go index c618353..25b5036 100644 --- a/internal/router/handlers/lang.go +++ b/internal/router/handlers/lang.go @@ -53,7 +53,10 @@ func (r *HomeHandler) AddMeta(c *fiber.Ctx, supplements *router.Supplements, lan return []router.MetaField{ {Property: "og:title", Content: supplements.Localization[lang].HomePage.Header}, {Property: "og:description", Content: supplements.Localization[lang].HomePage.HomePageDescription}, - {Property: "og:image", Content: fmt.Sprintf("https://f003.backblazeb2.com/file/sayana-static/home-page-gifs/otter-%d.gif", rand.Int()%3+1)}, + {Property: "og:image", Content: fmt.Sprintf( + supplements.PhotoStorage.HomePageGifs.BaseUrl, + supplements.PhotoStorage.HomePageGifs.Indexes[rand.Int()%len(supplements.PhotoStorage.HomePageGifs.Indexes)], + )}, {Property: "og:url", Content: fmt.Sprintf("%s/%s", templateMap["CanonicalEndpoint"], lang)}, {Property: "og:type", Content: "website"}, }, nil @@ -63,7 +66,10 @@ func (r *HomeHandler) RenderBody(c *fiber.Ctx, supplements *router.Supplements, templateMap["Title"] = supplements.Localization[lang].HomePage.Header templateMap["FilledHeartCount"] = uint(40) templateMap["OutlineHeartCount"] = uint(40) - templateMap["GifName"] = fmt.Sprintf("otter-%d.gif", rand.Int()%3+1) + templateMap["GifUrl"] = fmt.Sprintf( + supplements.PhotoStorage.HomePageGifs.BaseUrl, + supplements.PhotoStorage.HomePageGifs.Indexes[rand.Int()%len(supplements.PhotoStorage.HomePageGifs.Indexes)], + ) templateMap["FunFacts"] = supplements.FactGiver.Give(lang) return fiber.StatusOK, nil } diff --git a/internal/router/router.go b/internal/router/router.go index e8fb47a..926d21a 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -101,6 +101,8 @@ type Supplements struct { TemplateManager *templatemanager.TemplateManager MarkdownRenderer goldmark.Markdown Meta config.MetaConfig + PhotoStorage config.PhotoStorageConfig + StaticStorage config.PhotoTypeConfig } type Router struct { @@ -156,7 +158,7 @@ func NewRouter(cfg *config.Config) (*Router, error) { supplements.MarkdownRenderer = goldmark.New( goldmark.WithExtensions( - glightbox.NewGLightboxExtension(), + glightbox.NewGLightboxExtension(cfg.PhotoStorage), tailwind.NewTailwindExtension(), ), goldmark.WithParserOptions( @@ -217,6 +219,8 @@ func NewRouter(cfg *config.Config) (*Router, error) { } supplements.Meta = cfg.Meta + supplements.PhotoStorage = cfg.PhotoStorage + supplements.StaticStorage = cfg.StaticStorage enablePrintRoutes := false if cfg.LogLevel <= slog.LevelDebug { @@ -229,7 +233,7 @@ func NewRouter(cfg *config.Config) (*Router, error) { }) app.Use(cors.New(cors.Config{ - AllowOrigins: "https://f003.backblazeb2.com", + AllowOrigins: strings.Join(cfg.AllowOrigins, ","), AllowMethods: "GET,POST,OPTIONS", AllowHeaders: "Origin, Content-Type, Accept", })) @@ -299,11 +303,13 @@ func (r *Router) InitRoutes() (err error) { } defaultMap := fiber.Map{ - "L": r.supplements.Localization[lang], - "Lang": lang, - "Path": trimmedPath, - "QueryString": queryString, - "CanonicalEndpoint": r.canonicalEndpoint, + "L": r.supplements.Localization[lang], + "Lang": lang, + "Path": trimmedPath, + "QueryString": queryString, + "CanonicalEndpoint": r.canonicalEndpoint, + "StaticStorageBaseUrl": r.supplements.StaticStorage.BaseUrl, + "ThumbnailBaseUrl": r.supplements.PhotoStorage.Thumbnail320p.BaseUrl, } statusCode, err := currentRoute.Render(c, r.supplements, lang, defaultMap) @@ -423,10 +429,12 @@ func (r *Router) generalPage(c *fiber.Ctx, route Route, lang string) error { } valueMap := fiber.Map{ - "L": r.supplements.Localization[lang], - "Lang": lang, - "QueryString": queryString, - "CanonicalEndpoint": r.canonicalEndpoint, + "L": r.supplements.Localization[lang], + "Lang": lang, + "QueryString": queryString, + "CanonicalEndpoint": r.canonicalEndpoint, + "StaticStorageBaseUrl": r.supplements.StaticStorage.BaseUrl, + "ThumbnailBaseUrl": r.supplements.PhotoStorage.Thumbnail320p.BaseUrl, } var err error @@ -503,11 +511,13 @@ func (r *Router) generalPageSegment(c *fiber.Ctx, part string) error { var statusCode int defaultMap := fiber.Map{ - "L": r.supplements.Localization[lang], - "Lang": lang, - "Path": strings.Trim(path, "/"), - "QueryString": queryString, - "CanonicalEndpoint": r.canonicalEndpoint, + "L": r.supplements.Localization[lang], + "Lang": lang, + "Path": strings.Trim(path, "/"), + "QueryString": queryString, + "CanonicalEndpoint": r.canonicalEndpoint, + "StaticStorageBaseUrl": r.supplements.StaticStorage.BaseUrl, + "ThumbnailBaseUrl": r.supplements.PhotoStorage.Thumbnail320p.BaseUrl, } switch part { diff --git a/views/layouts/general-page.html b/views/layouts/general-page.html index b620566..1265266 100644 --- a/views/layouts/general-page.html +++ b/views/layouts/general-page.html @@ -13,8 +13,8 @@ {{- end }} {{- end }} - - + + {{- with .LinkedData }} - + - - - + + + + - - + + +