forked from mtusi2026/mtusi2026lab3
feat(app) lab 3 implemented
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
// AGENT: All runtime configuration is read here so every other package receives
|
||||
// explicit values instead of calling os.Getenv. Values are compatible with
|
||||
// .env.example and support the *_FILE convention used by Compose secrets.
|
||||
// Package config loads and validates process configuration from environment
|
||||
// variables and optional *_FILE secrets.
|
||||
package config
|
||||
|
||||
import (
|
||||
@@ -12,25 +11,36 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
SessionTTL time.Duration
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
DBMaxConns int32
|
||||
DBTimeout time.Duration
|
||||
RedisTimeout time.Duration
|
||||
StartupWait time.Duration
|
||||
Host string
|
||||
Port string
|
||||
InstanceID string
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
DBMaxConns int32
|
||||
DBTimeout time.Duration
|
||||
RedisTimeout time.Duration
|
||||
StartupWait time.Duration
|
||||
RunMigrations bool
|
||||
MigrateOnly bool
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
instance, err := instanceID()
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
Host: envOr("APP_HOST", "0.0.0.0"),
|
||||
Port: envOr("APP_PORT", "8080"),
|
||||
DBMaxConns: 10,
|
||||
DBTimeout: 5 * time.Second,
|
||||
RedisTimeout: 2 * time.Second,
|
||||
StartupWait: 30 * time.Second,
|
||||
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,
|
||||
}
|
||||
|
||||
ttlSeconds, err := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600"))
|
||||
@@ -39,6 +49,19 @@ func Load() (Config, error) {
|
||||
}
|
||||
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
|
||||
}
|
||||
if cfg.MigrateOnly, err = envBool("MIGRATE_ONLY", false); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
if value := envOr("DB_POOL_MAX_CONNS", ""); value != "" {
|
||||
n, convErr := strconv.Atoi(value)
|
||||
if convErr != nil || n < 1 || n > 100 {
|
||||
@@ -55,6 +78,10 @@ func Load() (Config, error) {
|
||||
return cfg, 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
|
||||
@@ -76,6 +103,32 @@ func Load() (Config, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func instanceID() (string, error) {
|
||||
if value := strings.TrimSpace(os.Getenv("INSTANCE_ID")); value != "" {
|
||||
return value, nil
|
||||
}
|
||||
name, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("determine INSTANCE_ID: %w", err)
|
||||
}
|
||||
if name = strings.TrimSpace(name); name == "" {
|
||||
return "", fmt.Errorf("determine INSTANCE_ID: hostname is empty and INSTANCE_ID is not set")
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func envBool(name string, fallback bool) (bool, error) {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
parsed, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s must be true or false", name)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func envOr(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInstanceIDUsesConfiguredValue(t *testing.T) {
|
||||
t.Setenv("INSTANCE_ID", " api-1 ")
|
||||
|
||||
got, err := instanceID()
|
||||
if err != nil {
|
||||
t.Fatalf("instanceID: %v", err)
|
||||
}
|
||||
if got != "api-1" {
|
||||
t.Fatalf("instanceID = %q, want api-1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceIDFallsBackToHostname(t *testing.T) {
|
||||
t.Setenv("INSTANCE_ID", " ")
|
||||
|
||||
got, err := instanceID()
|
||||
if err != nil {
|
||||
t.Fatalf("instanceID: %v", err)
|
||||
}
|
||||
if got == "" {
|
||||
t.Fatal("instanceID returned an empty value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user