From b734ea7d3a5934a3ca05ad36827de87fb9e92c36 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sun, 26 Apr 2026 00:15:45 +0300 Subject: [PATCH] refactor: Add environment variable support to API server configuration and fix build path - Add getEnv and getEnvInt helper functions for reading environment variables with defaults - Update all configuration fields to use environment variables (DB_HOST, REDIS_HOST, API_PORT, etc.) - Change CI build command from ./cmd/server.go to ./cmd/ directory path --- .github/workflows/ci.yml | 2 +- apps/api-server/internal/config/config.go | 64 +++++++++++++++-------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 934db4a..2c1be22 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,7 +65,7 @@ jobs: - name: Build API server working-directory: apps/api-server - run: go build -o bin/api-server ./cmd/server.go + run: go build -o bin/api-server ./cmd/ web-dashboard-test: name: Web Dashboard Tests diff --git a/apps/api-server/internal/config/config.go b/apps/api-server/internal/config/config.go index e9a8584..2093344 100644 --- a/apps/api-server/internal/config/config.go +++ b/apps/api-server/internal/config/config.go @@ -1,6 +1,8 @@ package config import ( + "os" + "strconv" "time" ) @@ -75,27 +77,43 @@ type AuthConfig struct { JWTSecret string `json:"jwt_secret"` } +func getEnv(key, defaultValue string) string { + if value := os.Getenv(key); value != "" { + return value + } + return defaultValue +} + +func getEnvInt(key string, defaultValue int) int { + if value := os.Getenv(key); value != "" { + if intVal, err := strconv.Atoi(value); err == nil { + return intVal + } + } + return defaultValue +} + func LoadConfig() *Config { return &Config{ Server: ServerConfig{ - Port: "8000", - MetricsPort: "9090", + Port: getEnv("API_PORT", "8000"), + MetricsPort: getEnv("METRICS_PORT", "9090"), ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, }, Database: DatabaseConfig{ - Host: "localhost", - Port: "5432", - Database: "telecom_platform", - Username: "postgres", - Password: "password", - SSLMode: "disable", + Host: getEnv("DB_HOST", "localhost"), + Port: getEnv("DB_PORT", "5432"), + Database: getEnv("DB_NAME", "telecom_platform"), + Username: getEnv("DB_USER", "postgres"), + Password: getEnv("DB_PASSWORD", "password"), + SSLMode: getEnv("DB_SSLMODE", "disable"), }, Redis: RedisConfig{ - Host: "localhost", - Port: "6379", - Password: "", - DB: 0, + Host: getEnv("REDIS_HOST", "localhost"), + Port: getEnv("REDIS_PORT", "6379"), + Password: getEnv("REDIS_PASSWORD", ""), + DB: getEnvInt("REDIS_DB", 0), }, IMSI: IMSIConfig{ Prefix: "20893", // France MCC+MNC @@ -103,28 +121,28 @@ func LoadConfig() *Config { MaxIMSI: 999999999, }, ES2: ES2Config{ - BaseURL: "https://smdp.example.com", - APIKey: "", + BaseURL: getEnv("ES2_BASE_URL", "https://smdp.example.com"), + APIKey: getEnv("ES2_API_KEY", ""), InsecureSkipVerify: true, FunctionalityRequesterID: "telecom-platform", }, Payment: PaymentConfig{ - Provider: "stripe", - StripeAPIKey: "", - StripeWebhookSecret: "", + Provider: getEnv("PAYMENT_PROVIDER", "stripe"), + StripeAPIKey: getEnv("STRIPE_API_KEY", ""), + StripeWebhookSecret: getEnv("STRIPE_WEBHOOK_SECRET", ""), }, ChargingEngine: ChargingEngineConfig{ - BaseURL: "http://localhost:3001", + BaseURL: getEnv("CHARGING_ENGINE_URL", "http://localhost:3001"), Timeout: 10 * time.Second, }, InfluxDB: InfluxDBConfig{ - URL: "http://localhost:8086", - Token: "", - Org: "telecom", - Bucket: "telecom", + URL: getEnv("INFLUXDB_URL", "http://localhost:8086"), + Token: getEnv("INFLUXDB_TOKEN", ""), + Org: getEnv("INFLUXDB_ORG", "telecom"), + Bucket: getEnv("INFLUXDB_BUCKET", "telecom"), }, Auth: AuthConfig{ - JWTSecret: "change-me-in-production", + JWTSecret: getEnv("JWT_SECRET", "change-me-in-production"), }, } }