forked from mtusi2026/mtusi2026lab3
263 lines
7.7 KiB
Go
263 lines
7.7 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config 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
|
|
}
|
|
|
|
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 databaseConfig struct {
|
|
url string
|
|
maxConns int
|
|
minConns int
|
|
maxConnectionAge time.Duration
|
|
maxConnectionIdle time.Duration
|
|
healthCheckPeriod time.Duration
|
|
dependencyTimeout time.Duration
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
values := viper.New()
|
|
values.AutomaticEnv()
|
|
|
|
host, err := requiredString(values, "APP_HOST")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
port, err := requiredPort(values, "APP_PORT")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
database, err := loadDatabaseConfig(values)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
cacheURL, err := requiredString(values, "CACHE_URL")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
store, err := requiredString(values, "SESSION_STORE")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
if store != "valkey" {
|
|
return Config{}, errors.New("SESSION_STORE must be valkey")
|
|
}
|
|
|
|
sessionTTL, err := seconds(values, "SESSION_TTL_SECONDS", 1, 86400*30)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
idempotencyTTL, err := seconds(values, "IDEMPOTENCY_TTL_SECONDS", 86400, 86400*30)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
requestTimeout, err := seconds(values, "REQUEST_TIMEOUT_SECONDS", 1, 300)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
readHeaderTimeout, err := seconds(values, "HTTP_READ_HEADER_TIMEOUT_SECONDS", 1, 300)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
readTimeout, err := seconds(values, "HTTP_READ_TIMEOUT_SECONDS", 1, 300)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
writeTimeout, err := seconds(values, "HTTP_WRITE_TIMEOUT_SECONDS", 1, 300)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
idleTimeout, err := seconds(values, "HTTP_IDLE_TIMEOUT_SECONDS", 1, 3600)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
shutdownTimeout, err := seconds(values, "HTTP_SHUTDOWN_TIMEOUT_SECONDS", 1, 300)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
passwordMemory, err := integer(values, "PASSWORD_MEMORY_KIB", 8*1024, 1024*1024)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
passwordIterations, err := integer(values, "PASSWORD_ITERATIONS", 1, 20)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
passwordParallelism, err := integer(values, "PASSWORD_PARALLELISM", 1, 255)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
passwordSaltLength, err := integer(values, "PASSWORD_SALT_LENGTH", 8, 1024)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
passwordKeyLength, err := integer(values, "PASSWORD_KEY_LENGTH", 16, 1024)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
swaggerPort, err := requiredPort(values, "SWAGGER_PORT")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
return Config{
|
|
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,
|
|
}, nil
|
|
}
|
|
|
|
func LoadMigration() (MigrationConfig, error) {
|
|
values := viper.New()
|
|
values.AutomaticEnv()
|
|
database, err := loadDatabaseConfig(values)
|
|
if err != nil {
|
|
return MigrationConfig{}, err
|
|
}
|
|
migrationTimeout, err := seconds(values, "MIGRATION_TIMEOUT_SECONDS", 1, 600)
|
|
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 loadDatabaseConfig(values *viper.Viper) (databaseConfig, error) {
|
|
databaseURL, err := requiredString(values, "DATABASE_URL")
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
maxConns, err := integer(values, "DB_MAX_CONNS", 1, 100)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
minConns, err := integer(values, "DB_MIN_CONNS", 0, maxConns)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
maxConnectionAge, err := seconds(values, "DB_MAX_CONNECTION_AGE_SECONDS", 1, 86400)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
maxConnectionIdle, err := seconds(values, "DB_MAX_CONNECTION_IDLE_SECONDS", 1, 86400)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
healthCheckPeriod, err := seconds(values, "DB_HEALTH_CHECK_SECONDS", 1, 3600)
|
|
if err != nil {
|
|
return databaseConfig{}, err
|
|
}
|
|
dependencyTimeout, err := seconds(values, "DEPENDENCY_TIMEOUT_SECONDS", 1, 60)
|
|
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 seconds(values *viper.Viper, name string, minimum, maximum int) (time.Duration, error) {
|
|
value, err := integer(values, name, minimum, maximum)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return time.Duration(value) * time.Second, 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
|
|
}
|