Skip to content

Commit 16a0cfc

Browse files
authored
🔗 docs: add proxy configuration setup (#23)
1 parent 2c7675f commit 16a0cfc

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,55 @@ pnpm dev
8181
VITE_MOONSHOT_KEY=你的_Moonshot_API_Key # 通常以 `sk-` 开头,如 `sk-xxxxxx`
8282
```
8383

84+
85+
## 🛠️ API 代理配置说明
86+
87+
本项目采用纯前端架构,所有后端服务均由外部或本地其他服务提供。为解决开发环境中的跨域问题,项目使用了 `Vite` 的代理功能 `server.proxy`(详见[官方文档](https://vite.dev/config/server-options.html#server-proxy))
88+
89+
以下是当前仓库的[代理配置](./vite.config.ts#L21)
90+
91+
```ts
92+
server: {
93+
// ...
94+
proxy: {
95+
'/spark': {
96+
target: 'https://spark-api-open.xf-yun.com',
97+
changeOrigin: true,
98+
ws: true,
99+
rewrite: (path) => path.replace(/^\/spark/, '')
100+
},
101+
'/siliconflow': {
102+
target: 'https://api.siliconflow.cn',
103+
changeOrigin: true,
104+
ws: true,
105+
rewrite: (path) => path.replace(/^\/siliconflow/, '')
106+
},
107+
'/moonshot': {
108+
target: 'https://api.moonshot.cn',
109+
changeOrigin: true,
110+
ws: true,
111+
rewrite: (path) => path.replace(/^\/moonshot/, '')
112+
}
113+
}
114+
},
115+
// ...
116+
```
117+
118+
### 注意事项
119+
120+
1. **环境限制**: 该代理配置仅在开发环境(`development`)中生效。若生产环境部署时请根据实际情况调整服务器配置
121+
122+
2. **路径匹配**: 请求路径需要与配置的代理路径前缀匹配,例如本地访问 `/spark/v1/chat/completions` 会被直接代理到 `https://spark-api-open.xf-yun.com/v1/chat/completions`
123+
124+
### 生产环境部署
125+
126+
生产环境建议使用以下方案之一:
127+
128+
- 配置正确的 `CORS` 响应头
129+
- 使用 `Nginx` 反向代理
130+
- 统一域名和端口,避免跨域问题
131+
132+
84133
## 🌍 模拟/真实 API 模式切换
85134

86135
本项目提供了一个模拟开发模式,用于在本地开发环境或 Github 等部署环境中模拟调用大模型相关策略,无需调用真实 API 接口。该模式在 [src/config/env.ts](src/config/env.ts) 文件中定义,由以下代码控制:

0 commit comments

Comments
 (0)