[breaking-change] Add default config values where appropriate.

This commit is contained in:
Catherine
2025-09-21 23:08:27 +00:00
parent 51606aac98
commit bf2922f892
4 changed files with 42 additions and 59 deletions
@@ -1,7 +1,9 @@
[listen]
pages = "tcp/:3000"
caddy = "tcp/:3001"
health = "tcp/:3002"
# log-format = "datetime+message"
# [listen]
# pages = "tcp/:3000"
# caddy = "tcp/:3001" # or "-" to disable
# health = "tcp/:3002"
# [[wildcard]]
# domain = "codeberg.page"
@@ -9,11 +11,11 @@ health = "tcp/:3002"
# index-repos = ["<user>.codeberg.page", "pages"]
# fallback-proxy-to = "https://codeberg.page"
[backend]
type = "fs"
# [backend]
# type = "fs"
[backend.fs]
root = "data"
# [backend.fs]
# root = "./data"
# [backend.s3]
# endpoint = "play.min.io"
+11 -38
View File
@@ -39,32 +39,19 @@ type S3Backend struct {
siteCache *otter.Cache[string, *CachedManifest]
}
func defaultCacheConfig[K comparable, V any](
func makeCacheOptions[K comparable, V any](
config CacheConfig,
maxAge time.Duration,
maxSize uint64,
weigher func(K, V) uint32,
) (*otter.Options[K, V], error) {
var err error
if config.MaxAge != "" {
maxAge, err = time.ParseDuration(config.MaxAge)
if err != nil {
return nil, fmt.Errorf("max-age: %w", err)
}
}
if config.MaxSize != 0 {
maxSize = config.MaxSize
}
) *otter.Options[K, V] {
options := &otter.Options[K, V]{}
if maxSize != 0 {
options.MaximumWeight = maxSize
if config.MaxSize != 0 {
options.MaximumWeight = config.MaxSize.Bytes()
options.Weigher = weigher
}
if maxAge != 0 {
options.ExpiryCalculator = otter.ExpiryWriting[K, V](maxAge)
if config.MaxAge != 0 {
options.ExpiryCalculator = otter.ExpiryWriting[K, V](time.Duration(config.MaxAge) * time.Second)
}
return options, nil
return options
}
func NewS3Backend(
@@ -102,28 +89,14 @@ func NewS3Backend(
}
}
blobCacheOptions, err := defaultCacheConfig(
config.Backend.S3.BlobCache,
0, 256*1048576,
func(key string, value *CachedBlob) uint32 { return uint32(len(value.blob)) })
blobCache, err := otter.New(makeCacheOptions(config.Backend.S3.BlobCache,
func(key string, value *CachedBlob) uint32 { return uint32(len(value.blob)) }))
if err != nil {
return nil, err
}
blobCache, err := otter.New(blobCacheOptions)
if err != nil {
return nil, err
}
siteCacheOptions, err := defaultCacheConfig(
config.Backend.S3.SiteCache,
60*time.Second, 16*1048576,
func(key string, value *CachedManifest) uint32 { return value.weight })
if err != nil {
return nil, err
}
siteCache, err := otter.New(siteCacheOptions)
siteCache, err := otter.New(makeCacheOptions(config.Backend.S3.SiteCache,
func(key string, value *CachedManifest) uint32 { return value.weight }))
if err != nil {
return nil, err
}
+10 -10
View File
@@ -10,16 +10,16 @@ import (
)
type CacheConfig struct {
MaxSize uint64 `toml:"max-size"` // in bytes
MaxAge string `toml:"max-age"`
MaxSize datasize.ByteSize `toml:"max-size"`
MaxAge uint `toml:"max-age"` // in seconds
}
type Config struct {
LogFormat string `toml:"log-format"`
LogFormat string `toml:"log-format" default:"datetime+message"`
Listen struct {
Pages string `toml:"pages"`
Caddy string `toml:"caddy"`
Health string `toml:"health"`
Pages string `toml:"pages" default:"tcp/:3000"`
Caddy string `toml:"caddy" default:"tcp/:3001"`
Health string `toml:"health" default:"tcp/:3002"`
} `toml:"listen"`
Wildcard []struct {
Domain string `toml:"domain"`
@@ -28,9 +28,9 @@ type Config struct {
FallbackProxyTo string `toml:"fallback-proxy-to"`
} `toml:"wildcard"`
Backend struct {
Type string `toml:"type"`
Type string `toml:"type" default:"fs"`
FS struct {
Root string `toml:"root"`
Root string `toml:"root" default:"./data"`
} `toml:"fs"`
S3 struct {
Endpoint string `toml:"endpoint"`
@@ -39,8 +39,8 @@ type Config struct {
SecretAccessKey string `toml:"secret-access-key"`
Region string `toml:"region"`
Bucket string `toml:"bucket"`
BlobCache CacheConfig `toml:"blob-cache"`
SiteCache CacheConfig `toml:"site-cache"`
BlobCache CacheConfig `toml:"blob-cache" default:"{\"MaxSize\":\"256MB\"}"`
SiteCache CacheConfig `toml:"site-cache" default:"{\"MaxAge\":60,\"MaxSize\":\"16MB\"}"`
}
} `toml:"backend"`
Limits struct {
+11 -3
View File
@@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
@@ -25,7 +26,7 @@ func FeatureActive(feature string) bool {
}
func listen(name string, listen string) net.Listener {
if listen == "" {
if listen == "-" {
return nil
}
@@ -79,6 +80,7 @@ func main() {
InitObservability()
configPath := flag.String("config", "config.toml", "path to configuration file")
printConfig := flag.Bool("print-config", false, "print final configuration as JSON")
getManifest := flag.String("get-manifest", "", "retrieve manifest for web root as ProtoJSON")
flag.Parse()
@@ -87,10 +89,16 @@ func main() {
}
UpdateConfigEnv() // environment takes priority
if *printConfig {
configJSON, _ := json.MarshalIndent(&config, "", " ")
fmt.Println(string(configJSON))
return
}
switch config.LogFormat {
case "short":
case "message":
log.SetFlags(0)
default:
case "datetime+message":
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
}