417 lines
13 KiB
Go
417 lines
13 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type ServerConfig struct {
|
|
Address string
|
|
DatabaseURL string
|
|
CacheURL string
|
|
SessionTTL time.Duration
|
|
IdempotencyTTL time.Duration
|
|
DBMaxConns int32
|
|
DBMinConns int32
|
|
DBMaxConnectionAge time.Duration
|
|
DBMaxConnectionIdle time.Duration
|
|
DBHealthCheckPeriod time.Duration
|
|
DependencyTimeout time.Duration
|
|
RequestTimeout time.Duration
|
|
HTTPReadHeaderTimeout time.Duration
|
|
HTTPReadTimeout time.Duration
|
|
HTTPWriteTimeout time.Duration
|
|
HTTPIdleTimeout time.Duration
|
|
HTTPShutdownTimeout time.Duration
|
|
PasswordMemoryKiB uint32
|
|
PasswordIterations uint32
|
|
PasswordParallelism uint8
|
|
PasswordSaltLength uint32
|
|
PasswordKeyLength uint32
|
|
SwaggerPort string
|
|
S3Endpoint string
|
|
S3Region string
|
|
S3Bucket string
|
|
S3AccessKey string
|
|
S3SecretKey string
|
|
S3UseTLS bool
|
|
S3Timeout time.Duration
|
|
AttachmentMaxBytes int64
|
|
AttachmentMaxPixels int64
|
|
AttachmentBodyMemory int64
|
|
AttachmentDecodeMemory int64
|
|
PipelineVersion int
|
|
}
|
|
|
|
type MigrationConfig struct {
|
|
DatabaseURL string
|
|
DBMaxConns int32
|
|
DBMinConns int32
|
|
DBMaxConnectionAge time.Duration
|
|
DBMaxConnectionIdle time.Duration
|
|
DBHealthCheckPeriod time.Duration
|
|
DependencyTimeout time.Duration
|
|
MigrationTimeout time.Duration
|
|
}
|
|
|
|
type ReconciliationConfig struct {
|
|
DatabaseURL string
|
|
DBMaxConns int32
|
|
DBMinConns int32
|
|
DBMaxConnectionAge time.Duration
|
|
DBMaxConnectionIdle time.Duration
|
|
DBHealthCheckPeriod time.Duration
|
|
DependencyTimeout time.Duration
|
|
S3Endpoint string
|
|
S3Region string
|
|
S3Bucket string
|
|
S3AccessKey string
|
|
S3SecretKey string
|
|
S3UseTLS bool
|
|
S3Timeout time.Duration
|
|
}
|
|
|
|
type databaseConfig struct {
|
|
url string
|
|
maxConns int
|
|
minConns int
|
|
maxConnectionAge time.Duration
|
|
maxConnectionIdle time.Duration
|
|
healthCheckPeriod time.Duration
|
|
dependencyTimeout time.Duration
|
|
}
|
|
|
|
type objectStorageConfig struct {
|
|
endpoint string
|
|
region string
|
|
bucket string
|
|
accessKey string
|
|
secretKey string
|
|
useTLS bool
|
|
timeout time.Duration
|
|
}
|
|
|
|
func LoadServer() (ServerConfig, error) {
|
|
values, err := loadValues()
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
|
|
host, err := requiredString(values, "server.host")
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
port, err := requiredPort(values, "server.port")
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
database, err := loadDatabaseConfig(values)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
cacheURL, err := requiredString(values, "cache.url")
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
store, err := requiredString(values, "server.session_store")
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
if store != "valkey" {
|
|
return ServerConfig{}, errors.New("server.session_store must be valkey")
|
|
}
|
|
|
|
sessionTTL, err := duration(values, "server.session_ttl", time.Second, 30*24*time.Hour)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
idempotencyTTL, err := duration(values, "server.idempotency_ttl", 24*time.Hour, 30*24*time.Hour)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
requestTimeout, err := duration(values, "server.request_timeout", time.Second, 5*time.Minute)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
readHeaderTimeout, err := duration(values, "server.http.read_header_timeout", time.Second, 5*time.Minute)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
readTimeout, err := duration(values, "server.http.read_timeout", time.Second, 5*time.Minute)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
writeTimeout, err := duration(values, "server.http.write_timeout", time.Second, 5*time.Minute)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
idleTimeout, err := duration(values, "server.http.idle_timeout", time.Second, time.Hour)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
shutdownTimeout, err := duration(values, "server.http.shutdown_timeout", time.Second, 5*time.Minute)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
passwordMemory, err := integer(values, "server.password.memory_kib", 8*1024, 1024*1024)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
passwordIterations, err := integer(values, "server.password.iterations", 1, 20)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
passwordParallelism, err := integer(values, "server.password.parallelism", 1, 255)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
passwordSaltLength, err := integer(values, "server.password.salt_length", 8, 1024)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
passwordKeyLength, err := integer(values, "server.password.key_length", 16, 1024)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
swaggerPort, err := requiredPort(values, "server.swagger_port")
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
storage, err := loadObjectStorageConfig(values)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
attachmentMaxBytes, err := integer(values, "attachments.max_bytes", 1, 10*1024*1024)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
attachmentMaxPixels, err := integer(values, "attachments.max_pixels", 1, 40_000_000)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
attachmentBodyMemory, err := integer(values, "attachments.body_memory_bytes", 1, 1024*1024*1024)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
if int64(attachmentBodyMemory) < int64(attachmentMaxBytes) {
|
|
return ServerConfig{}, fmt.Errorf("attachments.body_memory_bytes: must be at least %d", attachmentMaxBytes)
|
|
}
|
|
attachmentDecodeMemory, err := integer(values, "attachments.decode_memory_bytes", 1, 1024*1024*1024)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
minimumDecodeMemory := int64(attachmentMaxPixels) * 8
|
|
if int64(attachmentDecodeMemory) < minimumDecodeMemory {
|
|
return ServerConfig{}, fmt.Errorf("attachments.decode_memory_bytes: must be at least %d", minimumDecodeMemory)
|
|
}
|
|
pipelineVersion, err := integer(values, "pipeline.version", 1, 1000)
|
|
if err != nil {
|
|
return ServerConfig{}, err
|
|
}
|
|
|
|
return ServerConfig{
|
|
Address: net.JoinHostPort(host, port), DatabaseURL: database.url, CacheURL: cacheURL,
|
|
SessionTTL: sessionTTL, IdempotencyTTL: idempotencyTTL,
|
|
DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
|
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
|
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
|
RequestTimeout: requestTimeout,
|
|
HTTPReadHeaderTimeout: readHeaderTimeout, HTTPReadTimeout: readTimeout,
|
|
HTTPWriteTimeout: writeTimeout, HTTPIdleTimeout: idleTimeout,
|
|
HTTPShutdownTimeout: shutdownTimeout, PasswordMemoryKiB: uint32(passwordMemory),
|
|
PasswordIterations: uint32(passwordIterations), PasswordParallelism: uint8(passwordParallelism),
|
|
PasswordSaltLength: uint32(passwordSaltLength), PasswordKeyLength: uint32(passwordKeyLength),
|
|
SwaggerPort: swaggerPort, S3Endpoint: storage.endpoint, S3Region: storage.region, S3Bucket: storage.bucket,
|
|
S3AccessKey: storage.accessKey, S3SecretKey: storage.secretKey,
|
|
S3UseTLS: storage.useTLS, S3Timeout: storage.timeout,
|
|
AttachmentMaxBytes: int64(attachmentMaxBytes), AttachmentMaxPixels: int64(attachmentMaxPixels),
|
|
AttachmentBodyMemory: int64(attachmentBodyMemory),
|
|
AttachmentDecodeMemory: int64(attachmentDecodeMemory),
|
|
PipelineVersion: pipelineVersion,
|
|
}, nil
|
|
}
|
|
|
|
func LoadMigration() (MigrationConfig, error) {
|
|
values, err := loadValues()
|
|
if err != nil {
|
|
return MigrationConfig{}, err
|
|
}
|
|
database, err := loadDatabaseConfig(values)
|
|
if err != nil {
|
|
return MigrationConfig{}, err
|
|
}
|
|
migrationTimeout, err := duration(values, "migration.timeout", time.Second, 10*time.Minute)
|
|
if err != nil {
|
|
return MigrationConfig{}, err
|
|
}
|
|
return MigrationConfig{
|
|
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
|
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
|
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
|
MigrationTimeout: migrationTimeout,
|
|
}, nil
|
|
}
|
|
|
|
func LoadReconciliation() (ReconciliationConfig, error) {
|
|
values, err := loadValues()
|
|
if err != nil {
|
|
return ReconciliationConfig{}, err
|
|
}
|
|
database, err := loadDatabaseConfig(values)
|
|
if err != nil {
|
|
return ReconciliationConfig{}, err
|
|
}
|
|
storage, err := loadObjectStorageConfig(values)
|
|
if err != nil {
|
|
return ReconciliationConfig{}, err
|
|
}
|
|
return ReconciliationConfig{
|
|
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
|
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
|
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
|
S3Endpoint: storage.endpoint, S3Region: storage.region, S3Bucket: storage.bucket,
|
|
S3AccessKey: storage.accessKey, S3SecretKey: storage.secretKey,
|
|
S3UseTLS: storage.useTLS, S3Timeout: storage.timeout,
|
|
}, nil
|
|
}
|
|
|
|
func loadObjectStorageConfig(values *viper.Viper) (objectStorageConfig, error) {
|
|
endpoint, err := requiredString(values, "object_storage.endpoint")
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
region, err := requiredString(values, "object_storage.region")
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
bucket, err := requiredString(values, "object_storage.bucket")
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
accessKey, err := requiredString(values, "object_storage.access_key")
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
secretKey, err := requiredString(values, "object_storage.secret_key")
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
useTLS, err := boolean(values, "object_storage.use_tls")
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
timeout, err := duration(values, "object_storage.timeout", time.Second, 5*time.Minute)
|
|
if err != nil {
|
|
return objectStorageConfig{}, err
|
|
}
|
|
return objectStorageConfig{
|
|
endpoint: endpoint, region: region, bucket: bucket, accessKey: accessKey,
|
|
secretKey: secretKey, useTLS: useTLS, timeout: timeout,
|
|
}, nil
|
|
}
|
|
|
|
func loadDatabaseConfig(values *viper.Viper) (databaseConfig, error) {
|
|
databaseURL, err := requiredString(values, "database.url")
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
maxConns, err := integer(values, "database.max_connections", 1, 100)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
minConns, err := integer(values, "database.min_connections", 0, maxConns)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
maxConnectionAge, err := duration(values, "database.max_connection_age", time.Second, 24*time.Hour)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
maxConnectionIdle, err := duration(values, "database.max_connection_idle", time.Second, 24*time.Hour)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
healthCheckPeriod, err := duration(values, "database.health_check_period", time.Second, time.Hour)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
dependencyTimeout, err := duration(values, "database.dependency_timeout", time.Second, time.Minute)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
return databaseConfig{
|
|
url: databaseURL, maxConns: maxConns, minConns: minConns,
|
|
maxConnectionAge: maxConnectionAge, maxConnectionIdle: maxConnectionIdle,
|
|
healthCheckPeriod: healthCheckPeriod, dependencyTimeout: dependencyTimeout,
|
|
}, nil
|
|
}
|
|
|
|
func requiredString(values *viper.Viper, name string) (string, error) {
|
|
value := strings.TrimSpace(values.GetString(name))
|
|
if value == "" {
|
|
return "", fmt.Errorf("%s is required", name)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func requiredPort(values *viper.Viper, name string) (string, error) {
|
|
value, err := requiredString(values, name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := integerValue(value, 1, 65535); err != nil {
|
|
return "", fmt.Errorf("%s: %w", name, err)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func duration(values *viper.Viper, name string, minimum, maximum time.Duration) (time.Duration, error) {
|
|
value, err := requiredString(values, name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil || parsed < minimum || parsed > maximum {
|
|
return 0, fmt.Errorf("%s: must be between %s and %s", name, minimum, maximum)
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
func integer(values *viper.Viper, name string, minimum, maximum int) (int, error) {
|
|
value, err := requiredString(values, name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
parsed, err := integerValue(value, minimum, maximum)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("%s: %w", name, err)
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
func integerValue(value string, minimum, maximum int) (int, error) {
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil || parsed < minimum || parsed > maximum {
|
|
return 0, fmt.Errorf("must be between %d and %d", minimum, maximum)
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
func boolean(values *viper.Viper, name string) (bool, error) {
|
|
value, err := requiredString(values, name)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
parsed, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
return false, fmt.Errorf("%s: must be true or false", name)
|
|
}
|
|
return parsed, nil
|
|
}
|