File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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" ,
Original file line number Diff line number Diff 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 :
You can’t perform that action at this time.
0 commit comments