Skip to content

Commit

Permalink
feat(pgs): Configure cache-control, TTL, max body, exclude /check (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-chaffee authored Dec 17, 2024
1 parent dfbc740 commit 6886a8e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 12 additions & 4 deletions pgs/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pgs

import (
"fmt"
"time"

"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)
Expand All @@ -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", "")
Expand All @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions pgs/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,26 @@ 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{
Enable: true,
},
},
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",
}
Expand Down
4 changes: 2 additions & 2 deletions shared/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6886a8e

Please sign in to comment.