Skip to content

Commit 1e40a0b

Browse files
committed
ci(docker): 添加环境变量以区分内部和公共API地址
在docker-compose.yml中添加了PUBLIC_API_BASE_URL和INTERNAL_API_BASE_URL环境变量,用于区分客户端和服务器端渲染时的API请求地址。同时,在axios.ts中根据环境变量动态设置baseURL,确保在不同环境下使用正确的API地址。
1 parent 133dceb commit 1e40a0b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

blog-web/src/lib/axios.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,26 @@ class Http {
2020
private instance: AxiosInstance;
2121
private baseURL: string;
2222

23-
constructor(baseURL: string = "https://api.exquisitecore.xyz/api") {
24-
this.baseURL = baseURL;
23+
constructor(baseURL?: string) {
24+
// 默认 API URL(如果未设置环境变量)
25+
const defaultApiUrl = "https://api.exquisitecore.xyz/api";
26+
let apiUrl;
27+
28+
// Astro 中通过 import.meta.env.SSR 判断是否为服务器端渲染
29+
if (import.meta.env.SSR) {
30+
// 服务器端渲染时,使用内部 Docker 网络地址
31+
// 这个环境变量需要在 Docker 容器中设置
32+
apiUrl = import.meta.env.INTERNAL_API_BASE_URL;
33+
} else {
34+
// 客户端渲染时,使用公共可访问地址
35+
// 这个环境变量也需要在 Docker 容器中设置(对于构建时)或由客户端环境提供
36+
apiUrl = import.meta.env.PUBLIC_API_BASE_URL;
37+
}
38+
39+
this.baseURL = baseURL || apiUrl || defaultApiUrl;
40+
2541
this.instance = axios.create({
26-
baseURL,
42+
baseURL: this.baseURL,
2743
timeout: 10000,
2844
headers: {
2945
"Content-Type": "application/json",

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ services:
3737
environment:
3838
- HOST=0.0.0.0
3939
- PORT=4321
40+
- PUBLIC_API_BASE_URL=http://localhost:8080/api
41+
- INTERNAL_API_BASE_URL=http://backend:8080/api
4042
ports:
4143
- "4321:4321"
4244
depends_on:

0 commit comments

Comments
 (0)