Update server.js
Browse files
server.js
CHANGED
|
@@ -1,13 +1,20 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
import { fal } from '@fal-ai/client';
|
| 3 |
|
| 4 |
-
// 从环境变量读取 Fal AI API Key
|
| 5 |
const FAL_KEY = process.env.FAL_KEY;
|
|
|
|
|
|
|
| 6 |
if (!FAL_KEY) {
|
| 7 |
console.error("Error: FAL_KEY environment variable is not set.");
|
| 8 |
process.exit(1);
|
| 9 |
}
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
// 配置 fal 客户端
|
| 12 |
fal.config({
|
| 13 |
credentials: FAL_KEY,
|
|
@@ -19,6 +26,33 @@ app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
|
| 19 |
|
| 20 |
const PORT = process.env.PORT || 3000;
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
// === 全局定义限制 ===
|
| 23 |
const PROMPT_LIMIT = 4800;
|
| 24 |
const SYSTEM_PROMPT_LIMIT = 4800;
|
|
@@ -68,7 +102,6 @@ app.get('/v1/models', (req, res) => {
|
|
| 68 |
}
|
| 69 |
});
|
| 70 |
|
| 71 |
-
|
| 72 |
// === 修改后的 convertMessagesToFalPrompt 函数 (System置顶 + 分隔符 + 对话历史Recency) ===
|
| 73 |
function convertMessagesToFalPrompt(messages) {
|
| 74 |
let fixed_system_prompt_content = "";
|
|
@@ -329,14 +362,15 @@ app.post('/v1/chat/completions', async (req, res) => {
|
|
| 329 |
}
|
| 330 |
});
|
| 331 |
|
| 332 |
-
//
|
| 333 |
-
app.listen(PORT,
|
| 334 |
console.log(`===================================================`);
|
| 335 |
console.log(` Fal OpenAI Proxy Server (System Top + Separator + Recency)`);
|
| 336 |
-
console.log(` Listening on
|
| 337 |
console.log(` Using Limits: System Prompt=${SYSTEM_PROMPT_LIMIT}, Prompt=${PROMPT_LIMIT}`);
|
| 338 |
console.log(` Fal AI Key Loaded: ${FAL_KEY ? 'Yes' : 'No'}`);
|
| 339 |
-
console.log(`
|
|
|
|
| 340 |
console.log(` Models Endpoint: GET http://localhost:${PORT}/v1/models`);
|
| 341 |
console.log(`===================================================`);
|
| 342 |
});
|
|
@@ -344,4 +378,4 @@ app.listen(PORT, '0.0.0.0', () => {
|
|
| 344 |
// 根路径响应 (更新信息)
|
| 345 |
app.get('/', (req, res) => {
|
| 346 |
res.send('Fal OpenAI Proxy (System Top + Separator + Recency Strategy) is running.');
|
| 347 |
-
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
import { fal } from '@fal-ai/client';
|
| 3 |
|
| 4 |
+
// 从环境变量读取 Fal AI API Key 和自定义 API Key
|
| 5 |
const FAL_KEY = process.env.FAL_KEY;
|
| 6 |
+
const API_KEY = process.env.API_KEY; // 添加自定义 API Key 环境变量
|
| 7 |
+
|
| 8 |
if (!FAL_KEY) {
|
| 9 |
console.error("Error: FAL_KEY environment variable is not set.");
|
| 10 |
process.exit(1);
|
| 11 |
}
|
| 12 |
|
| 13 |
+
if (!API_KEY) {
|
| 14 |
+
console.error("Error: API_KEY environment variable is not set.");
|
| 15 |
+
process.exit(1);
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
// 配置 fal 客户端
|
| 19 |
fal.config({
|
| 20 |
credentials: FAL_KEY,
|
|
|
|
| 26 |
|
| 27 |
const PORT = process.env.PORT || 3000;
|
| 28 |
|
| 29 |
+
// API Key 鉴权中间件
|
| 30 |
+
const apiKeyAuth = (req, res, next) => {
|
| 31 |
+
const authHeader = req.headers['authorization'];
|
| 32 |
+
|
| 33 |
+
if (!authHeader) {
|
| 34 |
+
console.warn('Unauthorized: No Authorization header provided');
|
| 35 |
+
return res.status(401).json({ error: 'Unauthorized: No API Key provided' });
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const authParts = authHeader.split(' ');
|
| 39 |
+
if (authParts.length !== 2 || authParts[0].toLowerCase() !== 'bearer') {
|
| 40 |
+
console.warn('Unauthorized: Invalid Authorization header format');
|
| 41 |
+
return res.status(401).json({ error: 'Unauthorized: Invalid Authorization header format' });
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
const providedKey = authParts[1];
|
| 45 |
+
if (providedKey !== API_KEY) {
|
| 46 |
+
console.warn('Unauthorized: Invalid API Key');
|
| 47 |
+
return res.status(401).json({ error: 'Unauthorized: Invalid API Key' });
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
next();
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
// 应用 API Key 鉴权中间件到所有 API 路由
|
| 54 |
+
app.use(['/v1/models', '/v1/chat/completions'], apiKeyAuth);
|
| 55 |
+
|
| 56 |
// === 全局定义限制 ===
|
| 57 |
const PROMPT_LIMIT = 4800;
|
| 58 |
const SYSTEM_PROMPT_LIMIT = 4800;
|
|
|
|
| 102 |
}
|
| 103 |
});
|
| 104 |
|
|
|
|
| 105 |
// === 修改后的 convertMessagesToFalPrompt 函数 (System置顶 + 分隔符 + 对话历史Recency) ===
|
| 106 |
function convertMessagesToFalPrompt(messages) {
|
| 107 |
let fixed_system_prompt_content = "";
|
|
|
|
| 362 |
}
|
| 363 |
});
|
| 364 |
|
| 365 |
+
// 启动服务器 (更新启动信息)
|
| 366 |
+
app.listen(PORT, () => {
|
| 367 |
console.log(`===================================================`);
|
| 368 |
console.log(` Fal OpenAI Proxy Server (System Top + Separator + Recency)`);
|
| 369 |
+
console.log(` Listening on port: ${PORT}`);
|
| 370 |
console.log(` Using Limits: System Prompt=${SYSTEM_PROMPT_LIMIT}, Prompt=${PROMPT_LIMIT}`);
|
| 371 |
console.log(` Fal AI Key Loaded: ${FAL_KEY ? 'Yes' : 'No'}`);
|
| 372 |
+
console.log(` API Key Auth Enabled: ${API_KEY ? 'Yes' : 'No'}`);
|
| 373 |
+
console.log(` Chat Completions Endpoint: POST http://localhost:${PORT}/v1/chat/completions`);
|
| 374 |
console.log(` Models Endpoint: GET http://localhost:${PORT}/v1/models`);
|
| 375 |
console.log(`===================================================`);
|
| 376 |
});
|
|
|
|
| 378 |
// 根路径响应 (更新信息)
|
| 379 |
app.get('/', (req, res) => {
|
| 380 |
res.send('Fal OpenAI Proxy (System Top + Separator + Recency Strategy) is running.');
|
| 381 |
+
});
|