Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d6de247

Browse files
committed
增加PATH_PREFIX常量
1 parent b3d9fab commit d6de247

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

.env.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PORT=3000
22
API_KEYS=sk-key1,sk-key2
3-
DEBUG_MODE=false
3+
DEBUG_MODE=false
4+
PATH_PREFIX=hf

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
<img src="https://vercel.com/button" style="height: 30px;"/></a>
1717
<br><br>
1818

19-
[中文](./doc/README_zh.md) | [English](./README_en.md)
19+
[中文](./doc/README_zh.md) | [English](./README_en.md)
2020
</div>
2121

22-
2322
## Supported Interfaces
2423

2524
- `GET - /v1/models`
@@ -35,12 +34,11 @@
3534

3635
## Environment variables
3736

38-
| Name | Option | Description |
39-
| ---------- | -------- | ------------------------------------------------ |
40-
| `PORT` | Optional | Request port, default is 3000 |
41-
| `API_KEYS` | Optional | API key group, separate multiple values with `,` |
42-
43-
37+
| Name | Option | Description |
38+
|---------------|----------|--------------------------------------------------------------------------------------------------------------------------------|
39+
| `PORT` | Optional | Request port, default is 3000 |
40+
| `API_KEYS` | Optional | API key group, separate multiple values with `,` |
41+
| `PATH_PREFIX` | Optional | The actual endpoint URL should be prefixed with `PATH_PREFIX` after configuration, example: `/PATH_PREFIX/v1/chat/completions` |
4442

4543
## Deployment
4644

@@ -49,15 +47,15 @@
4947
<a href="https://deploy.workers.cloudflare.com/?url=https://github.com/meethuhu/ddg2api/actions/workflows/workers-deploy.yml">
5048
<img src="https://deploy.workers.cloudflare.com/button" style="height: 30px;"/></a>
5149

52-
*suggest manually setting `API_KEYS`
50+
*suggest manually setting `API_KEYS`
5351
*To deploy manually, go to the `cf` branch
5452

5553
- ### Vercel
5654

5755
<a href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fmeethuhu%2FDDG2API">
5856
<img src="https://vercel.com/button" style="height: 30px;"/></a>
5957

60-
*suggest manually setting `API_KEYS`
58+
*suggest manually setting `API_KEYS`
6159

6260
- ### Docker
6361

@@ -99,6 +97,7 @@
9997
```
10098

10199
## Usage example
100+
102101
```shell
103102
# Get model list
104103
curl http://localhost:3000/v1/models \
@@ -119,10 +118,10 @@ curl http://localhost:3000/v1/chat/completions \
119118

120119
### Notes
121120

122-
1. This project is for learning and research purposes only
123-
2. Please comply with DuckDuckGo's terms of use
124-
3. Recommended for use in local or private environments
125-
4. Please keep your keys secure
121+
1. This project is for learning and research purposes only
122+
2. Please comply with DuckDuckGo's terms of use
123+
3. Recommended for use in local or private environments
124+
4. Please keep your keys secure
126125

127126
### Open Source License
128127

doc/README_zh.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535

3636
## 环境变量
3737

38-
| 名称 | 是否必需 | 描述 |
39-
| ---------- | -------- | ------------------------------ |
40-
| `PORT` | 可选 | 服务端口,默认为 3000 |
41-
| `API_KEYS` | 可选 | API密钥组,多个值用逗号(,)分隔 |
38+
| 名称 | 是否必需 | 描述 |
39+
| ---------- | -------- |----------------------------------------------------------------------|
40+
| `PORT` | 可选 | 服务端口,默认为 3000 |
41+
| `API_KEYS` | 可选 | API密钥组,多个值用逗号(,)分隔 |
42+
| `PATH_PREFIX` | 可选 | 配置后,实际端点 URL 应以“PATH_PREFIX”为前缀,例如:`/PATH_PREFIX/v1/chat/completions` |
4243

4344
## 部署方式
4445

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const PORT = process.env.PORT || 3000;
1616
const API_KEYS = new Set(process.env.API_KEYS ? process.env.API_KEYS.split(',') : []);
1717
// DEBUG 模式
1818
const DEBUG_MODE = process.env.DEBUG_MODE === 'true';
19+
// 自定义前缀
20+
const PATH_PREFIX = process.env.PATH_PREFIX?'/'+process.env.PATH_PREFIX:'';
21+
1922
// 模型映射
2023
const MODELS = {
2124
'gpt-4o-mini': 'gpt-4o-mini',
@@ -273,7 +276,7 @@ app.use(cors({
273276
app.options('*', cors());
274277

275278
// v1/models 路由
276-
app.get('/v1/models', validateApiKey, (req, res) => {
279+
app.get(PATH_PREFIX+'/v1/models', validateApiKey, (req, res) => {
277280
res.json({
278281
object: "list",
279282
data: Object.keys(MODELS).map(modelName => ({
@@ -285,7 +288,7 @@ app.get('/v1/models', validateApiKey, (req, res) => {
285288
});
286289

287290
// v1/chat/completions 路由
288-
app.post('/v1/chat/completions', validateApiKey, async (req, res, next) => {
291+
app.post(PATH_PREFIX+'/v1/chat/completions', validateApiKey, async (req, res, next) => {
289292
const {messages, model, stream = false} = req.body;
290293

291294
if (!messages?.length) {

0 commit comments

Comments
 (0)