Bootstrap milestone 0 and default API port 9999

This commit is contained in:
2026-02-04 22:00:29 +03:00
parent 166bc9623b
commit 1528fd654a
15 changed files with 312 additions and 9 deletions

68
internal/config/config.go Normal file
View File

@@ -0,0 +1,68 @@
package config
import (
"fmt"
"os"
"strconv"
"time"
)
// Config holds process configuration loaded from environment variables.
type Config struct {
HTTPAddr string
ReadTimeout time.Duration
WriteTimeout time.Duration
ShutdownGrace time.Duration
DatabaseDSN string
MigrationsDir string
}
func Load() (Config, error) {
readTimeout, err := envDuration("READ_TIMEOUT", 10*time.Second)
if err != nil {
return Config{}, err
}
writeTimeout, err := envDuration("WRITE_TIMEOUT", 15*time.Second)
if err != nil {
return Config{}, err
}
shutdownGrace, err := envDuration("SHUTDOWN_GRACE", 10*time.Second)
if err != nil {
return Config{}, err
}
return Config{
HTTPAddr: envOrDefault("HTTP_ADDR", ":9999"),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ShutdownGrace: shutdownGrace,
DatabaseDSN: os.Getenv("DATABASE_DSN"),
MigrationsDir: envOrDefault("MIGRATIONS_DIR", "migrations"),
}, nil
}
func envOrDefault(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func envDuration(key string, fallback time.Duration) (time.Duration, error) {
value := os.Getenv(key)
if value == "" {
return fallback, nil
}
seconds, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("%s must be an integer number of seconds: %w", key, err)
}
if seconds <= 0 {
return 0, fmt.Errorf("%s must be > 0", key)
}
return time.Duration(seconds) * time.Second, nil
}