From 66b2a96fcca113105043be3cb9c49f8ee7fe240c Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Wed, 15 Apr 2026 16:11:51 +0700 Subject: feat: support s3 for blog pages refactor: make blog client an interface feat: claude.md --- config/config.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index eb4a91d..e48a28b 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,8 @@ package config import ( + "encoding/json" + "fmt" "log/slog" "os" @@ -25,8 +27,72 @@ type BlogPagesConfig struct { } type StorageConfig struct { - Type string `json:"type" yaml:"type"` - Config B2Config `json:"Config" yaml:"config" validate:"required"` + Type string `json:"type" yaml:"type,oneof=b2 s3"` + Config any `json:"Config" yaml:"config" validate:"required"` +} + +func (sc *StorageConfig) UnmarshalJSON(data []byte) error { + var tmp struct { + Type string `json:"Type"` + Config json.RawMessage `json:"Config"` + } + + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + + sc.Type = tmp.Type + + switch tmp.Type { + case "b2": + var b2Config B2Config + if err := json.Unmarshal(tmp.Config, &b2Config); err != nil { + return fmt.Errorf("unmarshal B2Config: %w", err) + } + sc.Config = &b2Config + case "s3": + var s3Config S3Config + if err := json.Unmarshal(tmp.Config, &s3Config); err != nil { + return fmt.Errorf("unmarshal S3Config: %w", err) + } + sc.Config = &s3Config + default: + return fmt.Errorf("unsupported storage type: %s", tmp.Type) + } + + return nil +} + +func (sc *StorageConfig) UnmarshalYAML(value *yaml.Node) error { + var tmp struct { + Type string `yaml:"type"` + Config yaml.Node `yaml:"config"` + } + + if err := value.Decode(&tmp); err != nil { + return err + } + + sc.Type = tmp.Type + + switch tmp.Type { + case "b2": + var b2Config B2Config + if err := tmp.Config.Decode(&b2Config); err != nil { + return fmt.Errorf("unmarshal B2Config: %w", err) + } + sc.Config = &b2Config + case "s3": + var s3Config S3Config + if err := tmp.Config.Decode(&s3Config); err != nil { + return fmt.Errorf("unmarshal S3Config: %w", err) + } + sc.Config = &s3Config + default: + return fmt.Errorf("unsupported storage type: %s", tmp.Type) + } + + return nil } type B2Config struct { @@ -37,6 +103,15 @@ type B2Config struct { ApplicationKey string `json:"ApplicationKey" yaml:"applicationKey"` } +type S3Config struct { + BucketName string `json:"BucketName" yaml:"bucketName" validate:"required,min=1"` + Region string `json:"Region" yaml:"region" validate:"required,min=1"` + Prefix string `json:"Prefix" yaml:"prefix"` + Endpoint string `json:"Endpoint" yaml:"endpoint" validate:"required,url"` + AccessKeyID string `json:"AccessKeyID" yaml:"accessKeyID"` + SecretAccessKey string `json:"SecretAccessKey" yaml:"secretAccessKey"` +} + type FactGiverConfig struct { Storage StorageConfig `json:"Storage" yaml:"storage" validate:"required"` FactsFileName string `json:"FactsFileName" yaml:"factsFileName" validate:"required"` -- cgit v1.3.1+13 From ab557dd4df732c5d461bf7015f9eb0e761c982b7 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Wed, 15 Apr 2026 16:31:54 +0700 Subject: feat: add usepathstyle flag for s3 config refactor: use aws-sdk-go-v2/config package for configuring s3 client --- config/config.go | 3 ++- go.mod | 10 ++++++++++ go.sum | 14 ++++++++++++++ internal/blog/s3.go | 29 +++++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 5 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index e48a28b..ebe0367 100644 --- a/config/config.go +++ b/config/config.go @@ -107,7 +107,8 @@ type S3Config struct { BucketName string `json:"BucketName" yaml:"bucketName" validate:"required,min=1"` Region string `json:"Region" yaml:"region" validate:"required,min=1"` Prefix string `json:"Prefix" yaml:"prefix"` - Endpoint string `json:"Endpoint" yaml:"endpoint" validate:"required,url"` + Endpoint string `json:"Endpoint" yaml:"endpoint" validate:"url"` + UsePathStyle bool `json:"UsePathStyle"` AccessKeyID string `json:"AccessKeyID" yaml:"accessKeyID"` SecretAccessKey string `json:"SecretAccessKey" yaml:"secretAccessKey"` } diff --git a/go.mod b/go.mod index 228e579..373095d 100644 --- a/go.mod +++ b/go.mod @@ -17,10 +17,20 @@ require ( gopkg.in/yaml.v3 v3.0.1 ) +require ( + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.21 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.8.6 // indirect + github.com/aws/aws-sdk-go-v2/service/signin v1.0.9 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.30.15 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.19 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.41.10 // indirect +) + require ( github.com/andybalholm/brotli v1.2.1 // indirect github.com/aws/aws-sdk-go-v2 v1.41.5 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.8 // indirect + github.com/aws/aws-sdk-go-v2/config v1.32.14 github.com/aws/aws-sdk-go-v2/credentials v1.19.14 // indirect github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.21 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.21 // indirect diff --git a/go.sum b/go.sum index 50f54cb..44821e2 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,18 @@ github.com/aws/aws-sdk-go-v2 v1.41.5 h1:dj5kopbwUsVUVFgO4Fi5BIT3t4WyqIDjGKCangnV github.com/aws/aws-sdk-go-v2 v1.41.5/go.mod h1:mwsPRE8ceUUpiTgF7QmQIJ7lgsKUPQOUl3o72QBrE1o= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.8 h1:eBMB84YGghSocM7PsjmmPffTa+1FBUeNvGvFou6V/4o= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.8/go.mod h1:lyw7GFp3qENLh7kwzf7iMzAxDn+NzjXEAGjKS2UOKqI= +github.com/aws/aws-sdk-go-v2/config v1.32.14 h1:opVIRo/ZbbI8OIqSOKmpFaY7IwfFUOCCXBsUpJOwDdI= +github.com/aws/aws-sdk-go-v2/config v1.32.14/go.mod h1:U4/V0uKxh0Tl5sxmCBZ3AecYny4UNlVmObYjKuuaiOo= github.com/aws/aws-sdk-go-v2/credentials v1.19.14 h1:n+UcGWAIZHkXzYt87uMFBv/l8THYELoX6gVcUvgl6fI= github.com/aws/aws-sdk-go-v2/credentials v1.19.14/go.mod h1:cJKuyWB59Mqi0jM3nFYQRmnHVQIcgoxjEMAbLkpr62w= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.21 h1:NUS3K4BTDArQqNu2ih7yeDLaS3bmHD0YndtA6UP884g= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.21/go.mod h1:YWNWJQNjKigKY1RHVJCuupeWDrrHjRqHm0N9rdrWzYI= github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.21 h1:Rgg6wvjjtX8bNHcvi9OnXWwcE0a2vGpbwmtICOsvcf4= github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.21/go.mod h1:A/kJFst/nm//cyqonihbdpQZwiUhhzpqTsdbhDdRF9c= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.21 h1:PEgGVtPoB6NTpPrBgqSE5hE/o47Ij9qk/SEZFbUOe9A= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.21/go.mod h1:p+hz+PRAYlY3zcpJhPwXlLC4C+kqn70WIHwnzAfs6ps= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.6 h1:qYQ4pzQ2Oz6WpQ8T3HvGHnZydA72MnLuFK9tJwmrbHw= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.6/go.mod h1:O3h0IK87yXci+kg6flUKzJnWeziQUKciKrLjcatSNcY= github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.22 h1:rWyie/PxDRIdhNf4DzRk0lvjVOqFJuNnO8WwaIRVxzQ= github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.22/go.mod h1:zd/JsJ4P7oGfUhXn1VyLqaRZwPmZwg44Jf2dS84Dm3Y= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.7 h1:5EniKhLZe4xzL7a+fU3C2tfUN4nWIqlLesfrjkuPFTY= @@ -24,6 +30,14 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.21 h1:ZlvrNcHSFFWUR github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.21/go.mod h1:cv3TNhVrssKR0O/xxLJVRfd2oazSnZnkUeTf6ctUwfQ= github.com/aws/aws-sdk-go-v2/service/s3 v1.99.0 h1:hlSuz394kV0vhv9drL5lhuEFbEOEP1VyQpy15qWh1Pk= github.com/aws/aws-sdk-go-v2/service/s3 v1.99.0/go.mod h1:uoA43SdFwacedBfSgfFSjjCvYe8aYBS7EnU5GZ/YKMM= +github.com/aws/aws-sdk-go-v2/service/signin v1.0.9 h1:QKZH0S178gCmFEgst8hN0mCX1KxLgHBKKY/CLqwP8lg= +github.com/aws/aws-sdk-go-v2/service/signin v1.0.9/go.mod h1:7yuQJoT+OoH8aqIxw9vwF+8KpvLZ8AWmvmUWHsGQZvI= +github.com/aws/aws-sdk-go-v2/service/sso v1.30.15 h1:lFd1+ZSEYJZYvv9d6kXzhkZu07si3f+GQ1AaYwa2LUM= +github.com/aws/aws-sdk-go-v2/service/sso v1.30.15/go.mod h1:WSvS1NLr7JaPunCXqpJnWk1Bjo7IxzZXrZi1QQCkuqM= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.19 h1:dzztQ1YmfPrxdrOiuZRMF6fuOwWlWpD2StNLTceKpys= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.19/go.mod h1:YO8TrYtFdl5w/4vmjL8zaBSsiNp3w0L1FfKVKenZT7w= +github.com/aws/aws-sdk-go-v2/service/sts v1.41.10 h1:p8ogvvLugcR/zLBXTXrTkj0RYBUdErbMnAFFp12Lm/U= +github.com/aws/aws-sdk-go-v2/service/sts v1.41.10/go.mod h1:60dv0eZJfeVXfbT1tFJinbHrDfSJ2GZl4Q//OSSNAVw= github.com/aws/smithy-go v1.24.2 h1:FzA3bu/nt/vDvmnkg+R8Xl46gmzEDam6mZ1hzmwXFng= github.com/aws/smithy-go v1.24.2/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= diff --git a/internal/blog/s3.go b/internal/blog/s3.go index de4eb3a..86a6463 100644 --- a/internal/blog/s3.go +++ b/internal/blog/s3.go @@ -11,6 +11,7 @@ import ( "github.com/SayaAndy/saya-today-web/config" "github.com/SayaAndy/saya-today-web/internal/frontmatter" "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/s3" ) @@ -27,12 +28,32 @@ func NewS3Client(cfg *config.StorageConfig) (Client, error) { } s3cfg := cfg.Config.(*config.S3Config) - s3cl := s3.New(s3.Options{ - Region: s3cfg.Region, - BaseEndpoint: aws.String(s3cfg.Endpoint), - Credentials: credentials.NewStaticCredentialsProvider(s3cfg.AccessKeyID, s3cfg.SecretAccessKey, ""), + opts := []func(*awsconfig.LoadOptions) error{ + awsconfig.WithRegion(s3cfg.Region), + } + if s3cfg.AccessKeyID != "" && s3cfg.SecretAccessKey != "" { + opts = append(opts, awsconfig.WithCredentialsProvider( + credentials.NewStaticCredentialsProvider(s3cfg.AccessKeyID, s3cfg.SecretAccessKey, ""), + )) + } + + awsCfg, err := awsconfig.LoadDefaultConfig(context.Background(), opts...) + if err != nil { + return nil, fmt.Errorf("load AWS config: %w", err) + } + + var s3Opts []func(*s3.Options) + if s3cfg.Endpoint != "" { + s3Opts = append(s3Opts, func(o *s3.Options) { + o.BaseEndpoint = aws.String(s3cfg.Endpoint) + }) + } + s3Opts = append(s3Opts, func(o *s3.Options) { + o.UsePathStyle = s3cfg.UsePathStyle }) + s3cl := s3.NewFromConfig(awsCfg, s3Opts...) + return &S3Client{s3cfg.Prefix, s3cfg.BucketName, s3cl}, nil } -- cgit v1.3.1+13 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(-) (limited to 'config') 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 }} - + - - - + + + + - - + + + - - - -