a
This commit is contained in:
parent
eae8c9dae5
commit
f7fdc77758
|
|
@ -98,7 +98,8 @@ app = FastAPI(
|
||||||
)
|
)
|
||||||
|
|
||||||
# CORS配置(允许React前端访问)
|
# CORS配置(允许React前端访问)
|
||||||
cors_origins_str = os.getenv('CORS_ORIGINS', 'http://localhost:3000,http://localhost:5173,http://as.deepx1.com,http://asapi.deepx1.com,http://r.deepx1.com')
|
# 默认包含:本地开发端口、主前端域名、推荐查看器域名
|
||||||
|
cors_origins_str = os.getenv('CORS_ORIGINS', 'http://localhost:3000,http://localhost:3001,http://localhost:5173,http://as.deepx1.com,http://asapi.deepx1.com,http://r.deepx1.com,https://r.deepx1.com')
|
||||||
cors_origins = [origin.strip() for origin in cors_origins_str.split(',') if origin.strip()]
|
cors_origins = [origin.strip() for origin in cors_origins_str.split(',') if origin.strip()]
|
||||||
|
|
||||||
logger.info(f"CORS允许的源: {cors_origins}")
|
logger.info(f"CORS允许的源: {cors_origins}")
|
||||||
|
|
|
||||||
|
|
@ -20,16 +20,30 @@ cd recommendations-viewer
|
||||||
npm install
|
npm install
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. 配置API地址(可选)
|
### 2. 配置API地址
|
||||||
|
|
||||||
如果需要修改API地址,可以:
|
**开发环境(推荐使用代理):**
|
||||||
|
- 默认使用Vite代理,无需配置
|
||||||
|
- 代理配置在 `vite.config.js` 中,默认代理到 `http://asapi.deepx1.com`
|
||||||
|
- 如果遇到跨域问题,确保后端CORS配置包含了 `http://localhost:3001`
|
||||||
|
|
||||||
- 在项目根目录创建 `.env` 文件:
|
**生产环境(部署后):**
|
||||||
|
- **重要**:必须在项目根目录创建 `.env` 文件:
|
||||||
```
|
```
|
||||||
VITE_API_URL=http://your-api-url.com
|
VITE_API_URL=http://asapi.deepx1.com
|
||||||
```
|
```
|
||||||
|
或如果使用HTTPS:
|
||||||
|
```
|
||||||
|
VITE_API_URL=https://asapi.deepx1.com
|
||||||
|
```
|
||||||
|
- 构建时,Vite会将环境变量注入到代码中
|
||||||
|
- 确保后端CORS配置包含部署后的域名(如 `http://r.deepx1.com` 或 `https://r.deepx1.com`)
|
||||||
|
|
||||||
- 或者修改 `vite.config.js` 中的代理配置
|
**跨域问题排查:**
|
||||||
|
1. 开发环境:确保使用Vite代理(不要设置VITE_API_URL,或设置为空)
|
||||||
|
2. 生产环境:必须设置 `VITE_API_URL` 环境变量
|
||||||
|
3. 检查后端CORS配置是否包含前端部署域名
|
||||||
|
4. 查看浏览器控制台的错误信息
|
||||||
|
|
||||||
### 3. 启动开发服务器
|
### 3. 启动开发服务器
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
// API服务 - 只包含推荐查询功能
|
// API服务 - 只包含推荐查询功能
|
||||||
const API_BASE_URL = import.meta.env.VITE_API_URL || (import.meta.env.DEV ? '' : 'http://localhost:8000');
|
// 开发环境:使用Vite代理(空字符串),生产环境:使用环境变量或默认值
|
||||||
|
const API_BASE_URL = import.meta.env.VITE_API_URL || (import.meta.env.DEV ? '' : 'http://asapi.deepx1.com');
|
||||||
|
|
||||||
const buildUrl = (path) => {
|
const buildUrl = (path) => {
|
||||||
|
// 如果API_BASE_URL为空(开发环境使用代理),直接返回路径
|
||||||
|
if (!API_BASE_URL) {
|
||||||
|
return path.startsWith('/') ? path : `/${path}`;
|
||||||
|
}
|
||||||
const baseUrl = API_BASE_URL.endsWith('/') ? API_BASE_URL.slice(0, -1) : API_BASE_URL;
|
const baseUrl = API_BASE_URL.endsWith('/') ? API_BASE_URL.slice(0, -1) : API_BASE_URL;
|
||||||
const cleanPath = path.startsWith('/') ? path : `/${path}`;
|
const cleanPath = path.startsWith('/') ? path : `/${path}`;
|
||||||
return baseUrl ? `${baseUrl}${cleanPath}` : cleanPath;
|
return `${baseUrl}${cleanPath}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const api = {
|
export const api = {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user