chore: lab import lab4 backend and infrastructure as baseline
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
DatabaseURL string
|
||||
SessionStore string
|
||||
CacheURL string
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
ShutdownDrain time.Duration
|
||||
|
||||
S3Endpoint string
|
||||
S3Bucket string
|
||||
S3UseSSL bool
|
||||
S3Region string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
|
||||
MaxUploadBytes int64
|
||||
MaxImagePixels int64
|
||||
|
||||
ReconcilerEnabled bool
|
||||
ReconcilerInterval time.Duration
|
||||
ReconcilerGracePeriod time.Duration
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
dsn, err := readSecret("DATABASE_URL_FILE", "DATABASE_URL")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("database url: %w", err)
|
||||
}
|
||||
|
||||
cacheURL, err := readSecretOptional("CACHE_URL_FILE", "CACHE_URL")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cache url: %w", err)
|
||||
}
|
||||
|
||||
ttlSeconds, err := readInt("SESSION_TTL_SECONDS", 3600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SESSION_TTL_SECONDS: %w", err)
|
||||
}
|
||||
if ttlSeconds <= 0 {
|
||||
return nil, fmt.Errorf("SESSION_TTL_SECONDS должен быть положительным")
|
||||
}
|
||||
|
||||
idemSeconds, err := readInt("IDEMPOTENCY_TTL_SECONDS", 86400)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS: %w", err)
|
||||
}
|
||||
if idemSeconds <= 0 {
|
||||
return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS должен быть положительным")
|
||||
}
|
||||
|
||||
drainSeconds, err := readInt("SHUTDOWN_DRAIN_SECONDS", 3)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS: %w", err)
|
||||
}
|
||||
if drainSeconds < 0 {
|
||||
return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS не может быть отрицательным")
|
||||
}
|
||||
|
||||
store := strings.ToLower(getenv("SESSION_STORE", "postgres"))
|
||||
switch store {
|
||||
case "postgres", "redis", "valkey":
|
||||
default:
|
||||
return nil, fmt.Errorf("SESSION_STORE=%q не поддерживается", store)
|
||||
}
|
||||
|
||||
s3Endpoint := getenv("S3_ENDPOINT", "")
|
||||
s3Bucket := getenv("S3_BUCKET", "")
|
||||
s3UseSSL, err := readBool("S3_USE_SSL", false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("S3_USE_SSL: %w", err)
|
||||
}
|
||||
s3Region := getenv("S3_REGION", "us-east-1")
|
||||
|
||||
s3AccessKey, err := readSecretOptional("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 access key: %w", err)
|
||||
}
|
||||
s3SecretKey, err := readSecretOptional("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 secret key: %w", err)
|
||||
}
|
||||
|
||||
maxUploadBytes, err := readInt64("MAX_UPLOAD_BYTES", 10485760)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MAX_UPLOAD_BYTES: %w", err)
|
||||
}
|
||||
if maxUploadBytes <= 0 {
|
||||
return nil, fmt.Errorf("MAX_UPLOAD_BYTES должен быть положительным")
|
||||
}
|
||||
|
||||
maxImagePixels, err := readInt64("MAX_IMAGE_PIXELS", 40000000)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MAX_IMAGE_PIXELS: %w", err)
|
||||
}
|
||||
if maxImagePixels <= 0 {
|
||||
return nil, fmt.Errorf("MAX_IMAGE_PIXELS должен быть положительным")
|
||||
}
|
||||
|
||||
reconcilerEnabled, err := readBool("RECONCILER_ENABLED", true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RECONCILER_ENABLED: %w", err)
|
||||
}
|
||||
reconcilerIntervalSec, err := readInt("RECONCILER_INTERVAL_SECONDS", 300)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS: %w", err)
|
||||
}
|
||||
if reconcilerIntervalSec <= 0 {
|
||||
return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS должен быть положительным")
|
||||
}
|
||||
reconcilerGraceSec, err := readInt("RECONCILER_GRACE_SECONDS", 3600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS: %w", err)
|
||||
}
|
||||
if reconcilerGraceSec <= 0 {
|
||||
return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS должен быть положительным")
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Host: getenv("APP_HOST", "0.0.0.0"),
|
||||
Port: getenv("APP_PORT", "8080"),
|
||||
DatabaseURL: dsn,
|
||||
SessionStore: store,
|
||||
CacheURL: cacheURL,
|
||||
SessionTTL: time.Duration(ttlSeconds) * time.Second,
|
||||
IdempotencyTTL: time.Duration(idemSeconds) * time.Second,
|
||||
ShutdownDrain: time.Duration(drainSeconds) * time.Second,
|
||||
|
||||
S3Endpoint: s3Endpoint,
|
||||
S3Bucket: s3Bucket,
|
||||
S3UseSSL: s3UseSSL,
|
||||
S3Region: s3Region,
|
||||
S3AccessKey: s3AccessKey,
|
||||
S3SecretKey: s3SecretKey,
|
||||
|
||||
MaxUploadBytes: maxUploadBytes,
|
||||
MaxImagePixels: maxImagePixels,
|
||||
|
||||
ReconcilerEnabled: reconcilerEnabled,
|
||||
ReconcilerInterval: time.Duration(reconcilerIntervalSec) * time.Second,
|
||||
ReconcilerGracePeriod: time.Duration(reconcilerGraceSec) * time.Second,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Config) S3Configured() bool {
|
||||
return c.S3Endpoint != "" &&
|
||||
c.S3Bucket != "" &&
|
||||
c.S3AccessKey != "" &&
|
||||
c.S3SecretKey != ""
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func readInt(key string, def int) (int, error) {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("не целое число: %q", v)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func readInt64(key string, def int64) (int64, error) {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
n, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("не целое число: %q", v)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func readBool(key string, def bool) (bool, error) {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("не bool: %q", v)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func readSecret(fileEnv, envKey string) (string, error) {
|
||||
v, err := readSecretOptional(fileEnv, envKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if v == "" {
|
||||
return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func readSecretOptional(fileEnv, envKey string) (string, error) {
|
||||
if path := os.Getenv(fileEnv); path != "" {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("чтение %s=%s: %w", fileEnv, path, err)
|
||||
}
|
||||
s := strings.TrimSpace(string(b))
|
||||
if s == "" {
|
||||
return "", fmt.Errorf("файл %s пуст", path)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
return os.Getenv(envKey), nil
|
||||
}
|
||||
Reference in New Issue
Block a user