forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
@@ -32,72 +32,63 @@ func Load() (Config, error) {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
sessionTTL, err := envInt("SESSION_TTL_SECONDS", 3600, 1, 0)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
idempotencyTTL, err := envInt("IDEMPOTENCY_TTL_SECONDS", 86400, 1, 0)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConns, err := envInt("DB_POOL_MAX_CONNS", 10, 1, 100)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
Host: envOr("APP_HOST", "0.0.0.0"),
|
||||
Port: envOr("APP_PORT", "8080"),
|
||||
InstanceID: instance,
|
||||
DBMaxConns: 10,
|
||||
DBTimeout: 5 * time.Second,
|
||||
RedisTimeout: 2 * time.Second,
|
||||
StartupWait: 30 * time.Second,
|
||||
RunMigrations: true,
|
||||
Host: envOr("APP_HOST", "0.0.0.0"),
|
||||
Port: envOr("APP_PORT", "8080"),
|
||||
InstanceID: instance,
|
||||
SessionTTL: time.Duration(sessionTTL) * time.Second,
|
||||
IdempotencyTTL: time.Duration(idempotencyTTL) * time.Second,
|
||||
DBMaxConns: int32(maxConns),
|
||||
DBTimeout: 5 * time.Second,
|
||||
RedisTimeout: 2 * time.Second,
|
||||
StartupWait: 30 * time.Second,
|
||||
}
|
||||
|
||||
ttlSeconds, err := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600"))
|
||||
if err != nil || ttlSeconds < 1 {
|
||||
return cfg, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
|
||||
}
|
||||
cfg.SessionTTL = time.Duration(ttlSeconds) * time.Second
|
||||
|
||||
idempotencySeconds, err := strconv.Atoi(envOr("IDEMPOTENCY_TTL_SECONDS", "86400"))
|
||||
if err != nil || idempotencySeconds < 1 {
|
||||
return cfg, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS must be a positive integer")
|
||||
}
|
||||
cfg.IdempotencyTTL = time.Duration(idempotencySeconds) * time.Second
|
||||
|
||||
if cfg.RunMigrations, err = envBool("RUN_MIGRATIONS", true); err != nil {
|
||||
return cfg, err
|
||||
return Config{}, err
|
||||
}
|
||||
if cfg.MigrateOnly, err = envBool("MIGRATE_ONLY", false); err != nil {
|
||||
return cfg, err
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
if value := envOr("DB_POOL_MAX_CONNS", ""); value != "" {
|
||||
n, convErr := strconv.Atoi(value)
|
||||
if convErr != nil || n < 1 || n > 100 {
|
||||
return cfg, fmt.Errorf("DB_POOL_MAX_CONNS must be an integer between 1 and 100")
|
||||
}
|
||||
cfg.DBMaxConns = int32(n)
|
||||
}
|
||||
|
||||
cfg.DatabaseURL, err = secret("DATABASE_URL")
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
if cfg.DatabaseURL, err = secret("DATABASE_URL"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if cfg.DatabaseURL == "" {
|
||||
return cfg, fmt.Errorf("DATABASE_URL or DATABASE_URL_FILE is required")
|
||||
return Config{}, fmt.Errorf("DATABASE_URL or DATABASE_URL_FILE is required")
|
||||
}
|
||||
|
||||
if cfg.MigrateOnly {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
cfg.CacheURL, err = secret("CACHE_URL")
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
if cfg.CacheURL, err = secret("CACHE_URL"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if cfg.CacheURL == "" {
|
||||
cfg.CacheURL, err = secret("REDIS_URL")
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
if cfg.CacheURL, err = secret("REDIS_URL"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
}
|
||||
if cfg.CacheURL == "" {
|
||||
return cfg, fmt.Errorf("CACHE_URL or CACHE_URL_FILE is required")
|
||||
return Config{}, fmt.Errorf("CACHE_URL or CACHE_URL_FILE is required")
|
||||
}
|
||||
|
||||
if value := strings.ToLower(envOr("SESSION_STORE", "redis")); value != "redis" {
|
||||
return cfg, fmt.Errorf("SESSION_STORE must be redis (got %q)", value)
|
||||
return Config{}, fmt.Errorf("SESSION_STORE must be redis (got %q)", value)
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
@@ -129,6 +120,23 @@ func envBool(name string, fallback bool) (bool, error) {
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
// envInt parses an integer setting; max <= 0 means "no upper bound".
|
||||
func envInt(name string, fallback, min, max int) (int, error) {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed < min || (max > 0 && parsed > max) {
|
||||
if max > 0 {
|
||||
return 0, fmt.Errorf("%s must be an integer between %d and %d", name, min, max)
|
||||
}
|
||||
return 0, fmt.Errorf("%s must be an integer >= %d", name, min)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func envOr(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user