From f0e622ff331be3e283a23db4c79eb4de2467027d Mon Sep 17 00:00:00 2001 From: shenlan Date: Sun, 5 Oct 2025 21:50:57 +0800 Subject: [PATCH] Read default service URLs from runtime config (#409) --- .../config/runtime-service-config.yaml | 14 ++++++- ui/homepage/lib/serviceConfig.ts | 41 +++++++++++-------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/ui/homepage/config/runtime-service-config.yaml b/ui/homepage/config/runtime-service-config.yaml index ec71e75..4bccd18 100644 --- a/ui/homepage/config/runtime-service-config.yaml +++ b/ui/homepage/config/runtime-service-config.yaml @@ -2,16 +2,26 @@ defaultEnvironment: production defaults: accountService: baseUrl: http://localhost:8080 + serverService: + baseUrl: http://localhost:8090 environments: development: accountService: baseUrl: http://localhost:8080 + serverService: + baseUrl: http://localhost:8090 test: accountService: baseUrl: http://localhost:8080 + serverService: + baseUrl: http://localhost:8090 production: accountService: - baseUrl: https://account.svc.plus + baseUrl: http://localhost:8080 + serverService: + baseUrl: http://localhost:8090 production_tls: accountService: - baseUrl: https://account.svc.plus:8443 + baseUrl: http://localhost:8080 + serverService: + baseUrl: http://localhost:8090 diff --git a/ui/homepage/lib/serviceConfig.ts b/ui/homepage/lib/serviceConfig.ts index b614938..a339482 100644 --- a/ui/homepage/lib/serviceConfig.ts +++ b/ui/homepage/lib/serviceConfig.ts @@ -1,11 +1,12 @@ import runtimeServiceConfigSource from '../config/runtime-service-config.yaml' -type AccountServiceRuntimeConfig = { +type ServiceRuntimeConfig = { baseUrl?: string } type EnvironmentRuntimeConfig = { - accountService?: AccountServiceRuntimeConfig + accountService?: ServiceRuntimeConfig + serverService?: ServiceRuntimeConfig } type RuntimeServiceConfig = { @@ -61,12 +62,29 @@ function parseSimpleYaml(source: string): RuntimeServiceConfig { const runtimeServiceConfig = parseSimpleYaml(runtimeServiceConfigSource) -const DEFAULT_ACCOUNT_SERVICE_URL = 'https://account.svc.plus' -const DEFAULT_SERVER_SERVICE_URL = 'http://localhost:8090' - const runtimeEnvironments: Record = runtimeServiceConfig.environments ?? {} +type ServiceKey = keyof EnvironmentRuntimeConfig + +const FALLBACK_ACCOUNT_SERVICE_URL = 'http://localhost:8080' +const FALLBACK_SERVER_SERVICE_URL = 'http://localhost:8090' + +function getRuntimeServiceBaseUrl(serviceKey: ServiceKey): string | undefined { + const environmentName = resolveRuntimeEnvironment() + const runtimeDefaults = runtimeServiceConfig.defaults?.[serviceKey]?.baseUrl + const environmentValue = environmentName + ? runtimeEnvironments[environmentName]?.[serviceKey]?.baseUrl + : undefined + + return environmentValue ?? runtimeDefaults +} + +const DEFAULT_ACCOUNT_SERVICE_URL = + getRuntimeServiceBaseUrl('accountService') ?? FALLBACK_ACCOUNT_SERVICE_URL +const DEFAULT_SERVER_SERVICE_URL = + getRuntimeServiceBaseUrl('serverService') ?? FALLBACK_SERVER_SERVICE_URL + type RuntimeEnvironmentName = keyof typeof runtimeEnvironments function normalizeEnvKey(value?: string | null): string | undefined { @@ -104,16 +122,6 @@ function resolveRuntimeEnvironment(): RuntimeEnvironmentName | undefined { return undefined } -function getRuntimeAccountServiceBaseUrl(): string | undefined { - const environmentName = resolveRuntimeEnvironment() - const runtimeDefaults = runtimeServiceConfig.defaults?.accountService?.baseUrl - const environmentValue = environmentName - ? runtimeEnvironments[environmentName]?.accountService?.baseUrl - : undefined - - return environmentValue ?? runtimeDefaults -} - function readEnvValue(...keys: string[]): string | undefined { for (const key of keys) { const raw = process.env[key] @@ -133,8 +141,7 @@ function normalizeBaseUrl(baseUrl: string): string { export function getAccountServiceBaseUrl(): string { const configured = readEnvValue('ACCOUNT_SERVICE_URL', 'NEXT_PUBLIC_ACCOUNT_SERVICE_URL') - const runtimeConfigured = getRuntimeAccountServiceBaseUrl() - return normalizeBaseUrl(configured ?? runtimeConfigured ?? DEFAULT_ACCOUNT_SERVICE_URL) + return normalizeBaseUrl(configured ?? DEFAULT_ACCOUNT_SERVICE_URL) } export function getServerServiceBaseUrl(): string {