Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.

Commit 342a8d9

Browse files
committed
feat: Add env_var support in ServiceConfig for custom environment variable names
1 parent c6e4213 commit 342a8d9

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

cmd/gateway/main.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ServiceConfig struct {
3737
TargetURL string `yaml:"target_url"`
3838
StripPrefix string `yaml:"strip_prefix"`
3939
AuthRequired bool `yaml:"auth_required"`
40+
EnvVar string `yaml:"env_var"` // Optional: custom environment variable name
4041
}
4142

4243
// --- Global Logger ---
@@ -59,28 +60,28 @@ func loadConfig(path string) (*Config, error) {
5960
config.JWTSecret = secret
6061
}
6162

62-
// It maps the 'name' from config.yaml to the environment variable name
63-
// from docker-compose.yml.
64-
serviceURLEnvMap := map[string]string{
65-
"auth": "AUTH_SERVICE_URL",
66-
"users": "AUTH_SERVICE_URL", // "users" also points to the auth service
67-
"vehicles": "VEHICLE_SERVICE_URL",
68-
"appointments": "APPOINTMENTS_SERVICE_URL",
69-
"services": "PROJECT_SERVICE_URL", // As defined in docker-compose, this is the project service
70-
"projects": "PROJECT_SERVICE_URL", // "projects" also points to this service
71-
"time-logs": "TIME_LOGGING_SERVICE_URL",
72-
"payments": "PAYMENT_SERVICE_URL",
73-
"invoices": "PAYMENT_SERVICE_URL", // "invoices" also points to this service
74-
"admin": "ADMIN_SERVICE_URL",
75-
"websocket": "WS_SERVICE_URL",
76-
"ai": "AI_SERVICE_URL",
77-
}
78-
63+
// Override service URLs from environment variables
64+
// If env_var is specified in config, use it; otherwise generate from service name
7965
for i := range config.Services {
80-
if envVar, ok := serviceURLEnvMap[config.Services[i].Name]; ok {
81-
if url := os.Getenv(envVar); url != "" {
82-
config.Services[i].TargetURL = url
83-
}
66+
var envVarName string
67+
68+
// Use custom env_var if specified, otherwise auto-generate
69+
if config.Services[i].EnvVar != "" {
70+
envVarName = config.Services[i].EnvVar
71+
} else {
72+
// Auto-generate: convert "service-name" to "SERVICE_NAME_SERVICE_URL"
73+
// e.g., "time-logs" -> "TIME_LOGS_SERVICE_URL"
74+
normalizedName := strings.ToUpper(strings.ReplaceAll(config.Services[i].Name, "-", "_"))
75+
envVarName = normalizedName + "_SERVICE_URL"
76+
}
77+
78+
// Override target URL if environment variable is set
79+
if envURL := os.Getenv(envVarName); envURL != "" {
80+
config.Services[i].TargetURL = envURL
81+
logger.Info("Service URL overridden from environment",
82+
"service", config.Services[i].Name,
83+
"env_var", envVarName,
84+
"url", envURL)
8485
}
8586
}
8687

config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,84 @@ services:
1212
target_url: "http://localhost:8081"
1313
strip_prefix: "/api/v1/auth"
1414
auth_required: false # Login/Register must be public
15+
env_var: "AUTH_SERVICE_URL"
1516

1617
- name: "users"
1718
path_prefix: "/api/v1/users/"
1819
target_url: "http://localhost:8081" # Also points to the auth service
1920
strip_prefix: "/api/v1/users"
2021
auth_required: true
22+
env_var: "AUTH_SERVICE_URL" # Shares the same backend as auth
2123

2224
# --- Vehicle Service (Port 8082) ---
2325
- name: "vehicles"
2426
path_prefix: "/api/v1/vehicles/"
2527
target_url: "http://localhost:8082"
2628
strip_prefix: "/api/v1/vehicles"
2729
auth_required: true
30+
# env_var not specified - will auto-generate: VEHICLES_SERVICE_URL
2831

2932
# --- Appointment & Scheduling Service (Port 8083) ---
3033
- name: "appointments"
3134
path_prefix: "/api/v1/appointments/"
3235
target_url: "http://localhost:8083"
3336
strip_prefix: "/api/v1/appointments"
3437
auth_required: true
38+
# env_var not specified - will auto-generate: APPOINTMENTS_SERVICE_URL
3539

3640
# --- Service & Project Management Service (Port 8084) ---
3741
- name: "services"
3842
path_prefix: "/api/v1/services/"
3943
target_url: "http://localhost:8084"
4044
strip_prefix: "/api/v1/services"
4145
auth_required: true
46+
env_var: "PROJECT_SERVICE_URL" # Points to project service
4247

4348
- name: "projects"
4449
path_prefix: "/api/v1/projects/"
4550
target_url: "http://localhost:8084" # Also points to the project service
4651
strip_prefix: "/api/v1/projects"
4752
auth_required: true
53+
env_var: "PROJECT_SERVICE_URL" # Shares the same backend
4854

4955
# --- Time Logging Service (Port 8085) ---
5056
- name: "time-logs"
5157
path_prefix: "/api/v1/time-logs/"
5258
target_url: "http://localhost:8085"
5359
strip_prefix: "/api/v1/time-logs"
5460
auth_required: true
61+
# env_var not specified - will auto-generate: TIME_LOGS_SERVICE_URL
5562

5663
# --- Payment & Billing Service (Port 8086) ---
5764
- name: "payments"
5865
path_prefix: "/api/v1/payments/"
5966
target_url: "http://localhost:8086"
6067
strip_prefix: "/api/v1/payments"
6168
auth_required: true
69+
env_var: "PAYMENT_SERVICE_URL"
6270

6371
- name: "invoices"
6472
path_prefix: "/api/v1/invoices/"
6573
target_url: "http://localhost:8086" # Also points to the payment service
6674
strip_prefix: "/api/v1/invoices"
6775
auth_required: true
76+
env_var: "PAYMENT_SERVICE_URL" # Shares the same backend
6877

6978
# --- Admin & Reporting Service (Port 8087) ---
7079
- name: "admin"
7180
path_prefix: "/api/v1/admin/"
7281
target_url: "http://localhost:8087"
7382
strip_prefix: "/api/v1/admin"
7483
auth_required: true
84+
# env_var not specified - will auto-generate: ADMIN_SERVICE_URL
85+
86+
# --- Notification Service (Port 8088) ---
87+
- name: "notifications"
88+
path_prefix: "/api/v1/notifications/"
89+
target_url: "http://localhost:8088"
90+
strip_prefix: "/api/v1/notifications"
91+
auth_required: true
92+
# env_var not specified - will auto-generate: NOTIFICATIONS_SERVICE_URL
7593

7694
# --- Bonus Features (For Later Implementation) ---
7795
- name: "websocket"

0 commit comments

Comments
 (0)