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
This commit is contained in:
nutcas3
2026-04-26 00:15:45 +03:00
parent 302b761916
commit b734ea7d3a
2 changed files with 42 additions and 24 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ jobs:
- name: Build API server - name: Build API server
working-directory: apps/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: web-dashboard-test:
name: Web Dashboard Tests name: Web Dashboard Tests
+41 -23
View File
@@ -1,6 +1,8 @@
package config package config
import ( import (
"os"
"strconv"
"time" "time"
) )
@@ -75,27 +77,43 @@ type AuthConfig struct {
JWTSecret string `json:"jwt_secret"` 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 { func LoadConfig() *Config {
return &Config{ return &Config{
Server: ServerConfig{ Server: ServerConfig{
Port: "8000", Port: getEnv("API_PORT", "8000"),
MetricsPort: "9090", MetricsPort: getEnv("METRICS_PORT", "9090"),
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second,
}, },
Database: DatabaseConfig{ Database: DatabaseConfig{
Host: "localhost", Host: getEnv("DB_HOST", "localhost"),
Port: "5432", Port: getEnv("DB_PORT", "5432"),
Database: "telecom_platform", Database: getEnv("DB_NAME", "telecom_platform"),
Username: "postgres", Username: getEnv("DB_USER", "postgres"),
Password: "password", Password: getEnv("DB_PASSWORD", "password"),
SSLMode: "disable", SSLMode: getEnv("DB_SSLMODE", "disable"),
}, },
Redis: RedisConfig{ Redis: RedisConfig{
Host: "localhost", Host: getEnv("REDIS_HOST", "localhost"),
Port: "6379", Port: getEnv("REDIS_PORT", "6379"),
Password: "", Password: getEnv("REDIS_PASSWORD", ""),
DB: 0, DB: getEnvInt("REDIS_DB", 0),
}, },
IMSI: IMSIConfig{ IMSI: IMSIConfig{
Prefix: "20893", // France MCC+MNC Prefix: "20893", // France MCC+MNC
@@ -103,28 +121,28 @@ func LoadConfig() *Config {
MaxIMSI: 999999999, MaxIMSI: 999999999,
}, },
ES2: ES2Config{ ES2: ES2Config{
BaseURL: "https://smdp.example.com", BaseURL: getEnv("ES2_BASE_URL", "https://smdp.example.com"),
APIKey: "", APIKey: getEnv("ES2_API_KEY", ""),
InsecureSkipVerify: true, InsecureSkipVerify: true,
FunctionalityRequesterID: "telecom-platform", FunctionalityRequesterID: "telecom-platform",
}, },
Payment: PaymentConfig{ Payment: PaymentConfig{
Provider: "stripe", Provider: getEnv("PAYMENT_PROVIDER", "stripe"),
StripeAPIKey: "", StripeAPIKey: getEnv("STRIPE_API_KEY", ""),
StripeWebhookSecret: "", StripeWebhookSecret: getEnv("STRIPE_WEBHOOK_SECRET", ""),
}, },
ChargingEngine: ChargingEngineConfig{ ChargingEngine: ChargingEngineConfig{
BaseURL: "http://localhost:3001", BaseURL: getEnv("CHARGING_ENGINE_URL", "http://localhost:3001"),
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
}, },
InfluxDB: InfluxDBConfig{ InfluxDB: InfluxDBConfig{
URL: "http://localhost:8086", URL: getEnv("INFLUXDB_URL", "http://localhost:8086"),
Token: "", Token: getEnv("INFLUXDB_TOKEN", ""),
Org: "telecom", Org: getEnv("INFLUXDB_ORG", "telecom"),
Bucket: "telecom", Bucket: getEnv("INFLUXDB_BUCKET", "telecom"),
}, },
Auth: AuthConfig{ Auth: AuthConfig{
JWTSecret: "change-me-in-production", JWTSecret: getEnv("JWT_SECRET", "change-me-in-production"),
}, },
} }
} }