From 9f1443f0ac7399893bc9952e4b4cc5e812ad206c Mon Sep 17 00:00:00 2001 From: Mac Chaffee Date: Mon, 16 Dec 2024 21:07:22 -0500 Subject: [PATCH] feat(pgs): Configure cache-control, TTL, max body, exclude /check --- .env.example | 3 +-- pgs/config.go | 16 ++++++++++++---- pgs/web.go | 11 ++++++++--- shared/config.go | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 1de8bede..8246086f 100644 --- a/.env.example +++ b/.env.example @@ -118,8 +118,7 @@ PGS_DOMAIN=pgs.dev.pico.sh:3005 PGS_PROTOCOL=http PGS_STORAGE_DIR=.storage PGS_DEBUG=1 -PGS_CACHE_USER=testuser -PGS_CACHE_PASSWORD=password +PGS_CACHE_TTL=600s PICO_CADDYFILE=./caddy/Caddyfile.pico PICO_V4= diff --git a/pgs/config.go b/pgs/config.go index 04ff48c4..9101b09e 100644 --- a/pgs/config.go +++ b/pgs/config.go @@ -1,6 +1,9 @@ package pgs import ( + "fmt" + "time" + "github.com/picosh/pico/shared" "github.com/picosh/utils" ) @@ -16,8 +19,13 @@ func NewConfigSite() *shared.ConfigSite { port := utils.GetEnv("PGS_WEB_PORT", "3000") protocol := utils.GetEnv("PGS_PROTOCOL", "https") storageDir := utils.GetEnv("PGS_STORAGE_DIR", ".storage") - pgsCacheUser := utils.GetEnv("PGS_CACHE_USER", "") - pgsCachePass := utils.GetEnv("PGS_CACHE_PASSWORD", "") + cacheTTL, err := time.ParseDuration(utils.GetEnv("PGS_CACHE_TTL", "")) + if err != nil { + cacheTTL = 600 * time.Second + } + cacheControl := utils.GetEnv( + "PGS_CACHE_CONTROL", + fmt.Sprintf("max-age=%d", int(cacheTTL.Seconds()))) minioURL := utils.GetEnv("MINIO_URL", "") minioUser := utils.GetEnv("MINIO_ROOT_USER", "") minioPass := utils.GetEnv("MINIO_ROOT_PASSWORD", "") @@ -29,8 +37,8 @@ func NewConfigSite() *shared.ConfigSite { Protocol: protocol, DbURL: dbURL, StorageDir: storageDir, - CacheUser: pgsCacheUser, - CachePassword: pgsCachePass, + CacheTTL: cacheTTL, + CacheControl: cacheControl, MinioURL: minioURL, MinioUser: minioUser, MinioPass: minioPass, diff --git a/pgs/web.go b/pgs/web.go index 18872368..b8307fd6 100644 --- a/pgs/web.go +++ b/pgs/web.go @@ -61,8 +61,8 @@ func StartApiServer() { logger.Error("could not connect to object storage", "err", err.Error()) return } - - stale := configurationtypes.Duration{Duration: 1000 * time.Second} + ttl := configurationtypes.Duration{Duration: cfg.CacheTTL} + stale := configurationtypes.Duration{Duration: cfg.CacheTTL * 2} c := &middleware.BaseConfiguration{ API: configurationtypes.API{ Prometheus: configurationtypes.APIEndpoint{ @@ -70,12 +70,17 @@ func StartApiServer() { }, }, DefaultCache: &configurationtypes.DefaultCache{ - TTL: configurationtypes.Duration{Duration: 300 * time.Second}, + TTL: ttl, Stale: stale, Otter: configurationtypes.CacheProvider{ Uuid: fmt.Sprintf("OTTER-%s", stale), Configuration: map[string]interface{}{}, }, + Regex: configurationtypes.Regex{ + Exclude: "/check", + }, + MaxBodyBytes: uint64(cfg.MaxAssetSize), + DefaultCacheControl: cfg.CacheControl, }, LogLevel: "debug", } diff --git a/shared/config.go b/shared/config.go index f3421999..007bcb50 100644 --- a/shared/config.go +++ b/shared/config.go @@ -38,8 +38,8 @@ type ConfigSite struct { Protocol string DbURL string StorageDir string - CacheUser string - CachePassword string + CacheTTL time.Duration + CacheControl string MinioURL string MinioUser string MinioPass string