File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -20,10 +20,26 @@ class Http {
20
20
private instance : AxiosInstance ;
21
21
private baseURL : string ;
22
22
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
+
25
41
this . instance = axios . create ( {
26
- baseURL,
42
+ baseURL : this . baseURL ,
27
43
timeout : 10000 ,
28
44
headers : {
29
45
"Content-Type" : "application/json" ,
Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ services:
37
37
environment :
38
38
- HOST=0.0.0.0
39
39
- PORT=4321
40
+ - PUBLIC_API_BASE_URL=http://localhost:8080/api
41
+ - INTERNAL_API_BASE_URL=http://backend:8080/api
40
42
ports :
41
43
- " 4321:4321"
42
44
depends_on :
You can’t perform that action at this time.
0 commit comments