Read default service URLs from runtime config (#409)

This commit is contained in:
shenlan 2025-10-05 21:50:57 +08:00 committed by GitHub
parent ef8d643922
commit f0e622ff33
2 changed files with 36 additions and 19 deletions

View File

@ -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

View File

@ -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<string, EnvironmentRuntimeConfig> =
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 {