From a15a2eb88f3cd670dcebd78c8d667444f9d044ee Mon Sep 17 00:00:00 2001 From: shenlan Date: Tue, 30 Sep 2025 21:30:06 +0800 Subject: [PATCH] chore: centralize service endpoints configuration (#328) --- ui/homepage/app/api/askai/route.ts | 4 ++- ui/homepage/app/api/auth/login/route.ts | 4 ++- ui/homepage/app/api/auth/register/route.ts | 4 ++- ui/homepage/app/api/auth/session/route.ts | 4 ++- ui/homepage/components/AskAIButton.tsx | 3 +- ui/homepage/lib/knowledgeBase.ts | 4 ++- ui/homepage/lib/serviceConfig.ts | 42 ++++++++++++++++++++++ 7 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 ui/homepage/lib/serviceConfig.ts diff --git a/ui/homepage/app/api/askai/route.ts b/ui/homepage/app/api/askai/route.ts index 58b2a17..20c4114 100644 --- a/ui/homepage/app/api/askai/route.ts +++ b/ui/homepage/app/api/askai/route.ts @@ -1,6 +1,8 @@ +import { getServerServiceBaseUrl } from '@lib/serviceConfig' + export async function POST(req: Request) { const { question, history } = await req.json() - const apiBase = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8080' + const apiBase = getServerServiceBaseUrl() const res = await fetch(`${apiBase}/api/askai`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, diff --git a/ui/homepage/app/api/auth/login/route.ts b/ui/homepage/app/api/auth/login/route.ts index 404517e..2af9d86 100644 --- a/ui/homepage/app/api/auth/login/route.ts +++ b/ui/homepage/app/api/auth/login/route.ts @@ -1,6 +1,8 @@ import { NextRequest, NextResponse } from 'next/server' -const ACCOUNT_SERVICE_URL = process.env.ACCOUNT_SERVICE_URL || 'http://localhost:8080' +import { getAccountServiceBaseUrl } from '@lib/serviceConfig' + +const ACCOUNT_SERVICE_URL = getAccountServiceBaseUrl() const SESSION_COOKIE_NAME = 'account_session' async function authenticateWithAccountService(email: string, password: string) { diff --git a/ui/homepage/app/api/auth/register/route.ts b/ui/homepage/app/api/auth/register/route.ts index 131a350..16ce58c 100644 --- a/ui/homepage/app/api/auth/register/route.ts +++ b/ui/homepage/app/api/auth/register/route.ts @@ -1,6 +1,8 @@ import { NextRequest, NextResponse } from 'next/server' -const ACCOUNT_SERVICE_URL = process.env.ACCOUNT_SERVICE_URL || 'http://localhost:8080' +import { getAccountServiceBaseUrl } from '@lib/serviceConfig' + +const ACCOUNT_SERVICE_URL = getAccountServiceBaseUrl() async function registerWithAccountService(body: Record) { try { diff --git a/ui/homepage/app/api/auth/session/route.ts b/ui/homepage/app/api/auth/session/route.ts index bc0543e..7b73aa5 100644 --- a/ui/homepage/app/api/auth/session/route.ts +++ b/ui/homepage/app/api/auth/session/route.ts @@ -1,7 +1,9 @@ import { cookies } from 'next/headers' import { NextRequest, NextResponse } from 'next/server' -const ACCOUNT_SERVICE_URL = process.env.ACCOUNT_SERVICE_URL || 'http://localhost:8080' +import { getAccountServiceBaseUrl } from '@lib/serviceConfig' + +const ACCOUNT_SERVICE_URL = getAccountServiceBaseUrl() const SESSION_COOKIE_NAME = 'account_session' type AccountUser = { diff --git a/ui/homepage/components/AskAIButton.tsx b/ui/homepage/components/AskAIButton.tsx index 8ea47d1..d99b4a7 100644 --- a/ui/homepage/components/AskAIButton.tsx +++ b/ui/homepage/components/AskAIButton.tsx @@ -3,11 +3,12 @@ import { useState } from 'react' import { Bot } from 'lucide-react' import { AskAIDialog } from './AskAIDialog' +import { getServerServiceBaseUrl } from '@lib/serviceConfig' export function AskAIButton() { const [open, setOpen] = useState(false) const [minimized, setMinimized] = useState(false) - const apiBase = process.env.NEXT_PUBLIC_API_BASE_URL || '' + const apiBase = getServerServiceBaseUrl() return ( <> diff --git a/ui/homepage/lib/knowledgeBase.ts b/ui/homepage/lib/knowledgeBase.ts index 8336d89..136e68e 100644 --- a/ui/homepage/lib/knowledgeBase.ts +++ b/ui/homepage/lib/knowledgeBase.ts @@ -1,5 +1,7 @@ +import { getServerServiceBaseUrl } from './serviceConfig' + export async function fetchRelatedDocs(query: string): Promise { - const apiBase = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8080' + const apiBase = getServerServiceBaseUrl() try { const res = await fetch(`${apiBase}/api/rag/query`, { method: 'POST', diff --git a/ui/homepage/lib/serviceConfig.ts b/ui/homepage/lib/serviceConfig.ts new file mode 100644 index 0000000..ac7fd8b --- /dev/null +++ b/ui/homepage/lib/serviceConfig.ts @@ -0,0 +1,42 @@ +const DEFAULT_ACCOUNT_SERVICE_URL = 'http://localhost:8080' +const DEFAULT_SERVER_SERVICE_URL = 'http://localhost:8090' + +function readEnvValue(...keys: string[]): string | undefined { + for (const key of keys) { + const raw = process.env[key] + if (typeof raw === 'string') { + const trimmed = raw.trim() + if (trimmed.length > 0) { + return trimmed + } + } + } + return undefined +} + +function normalizeBaseUrl(baseUrl: string): string { + return baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl +} + +export function getAccountServiceBaseUrl(): string { + const configured = readEnvValue('ACCOUNT_SERVICE_URL', 'NEXT_PUBLIC_ACCOUNT_SERVICE_URL') + return normalizeBaseUrl(configured ?? DEFAULT_ACCOUNT_SERVICE_URL) +} + +export function getServerServiceBaseUrl(): string { + const configured = readEnvValue( + 'SERVER_SERVICE_URL', + 'NEXT_PUBLIC_SERVER_SERVICE_URL', + 'NEXT_PUBLIC_API_BASE_URL', + ) + return normalizeBaseUrl(configured ?? DEFAULT_SERVER_SERVICE_URL) +} + +export const serviceConfig = { + account: { + baseUrl: getAccountServiceBaseUrl(), + }, + server: { + baseUrl: getServerServiceBaseUrl(), + }, +} as const