From f271608114cb4e07bb1282f1d74699437c9fd952 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 17:44:40 +0800 Subject: [PATCH] feat: add dynamic base URL configuration using environment variables (#1511) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace hardcoded base URL in HttpClient.ts with environment variable support - Add NEXT_PUBLIC_API_BASE_URL environment variable for dynamic configuration - Add dev:local script for development with localhost:5300 backend - Development: uses localhost:5300, Production: uses / (relative path) - Eliminates need for manual code changes when switching environments Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Junyan Qin , 秦骏言 in Chinese, you can call me my english name Rock Chin. --- web/package.json | 1 + web/src/app/infra/http/HttpClient.ts | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/web/package.json b/web/package.json index 9c278e0d..6d1fca11 100644 --- a/web/package.json +++ b/web/package.json @@ -4,6 +4,7 @@ "private": true, "scripts": { "dev": "next dev --turbopack", + "dev:local": "NEXT_PUBLIC_API_BASE_URL=http://localhost:5300 next dev --turbopack", "build": "next build", "start": "next start", "lint": "next lint", diff --git a/web/src/app/infra/http/HttpClient.ts b/web/src/app/infra/http/HttpClient.ts index ce6e1bfc..a86cdbe8 100644 --- a/web/src/app/infra/http/HttpClient.ts +++ b/web/src/app/infra/http/HttpClient.ts @@ -494,9 +494,15 @@ class HttpClient { } } -// export const httpClient = new HttpClient('https://event-log.langbot.dev'); -// export const httpClient = new HttpClient('http://localhost:5300'); -export const httpClient = new HttpClient('/'); +const getBaseURL = (): string => { + if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_API_BASE_URL) { + return process.env.NEXT_PUBLIC_API_BASE_URL; + } + + return '/'; +}; + +export const httpClient = new HttpClient(getBaseURL()); // 临时写法,未来两种Client都继承自HttpClient父类,不允许共享方法 export const spaceClient = new HttpClient('https://space.langbot.app');