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
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
+41 -23
View File
@@ -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"),
},
}
}