dan92 commited on
Commit
863deaf
·
verified ·
1 Parent(s): ff08534

Upload 2 files

Browse files
Files changed (1) hide show
  1. src/index.js +67 -46
src/index.js CHANGED
@@ -19,6 +19,71 @@ app.use(bodyParser({
19
  textLimit: '30mb', // text 数据大小限制
20
  }))
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  const makeRequest = async (session_id, requestModel, messages) => {
23
  console.log('开始请求 genspark.ai:', { session_id, requestModel })
24
  try {
@@ -217,7 +282,7 @@ const handleChatCompletions = async (ctx) => {
217
  }
218
  }
219
 
220
- // 创建获取模型列表��处理函数
221
  const handleModels = async (ctx) => {
222
  ctx.body = {
223
  object: "list",
@@ -251,53 +316,9 @@ router.get('/', async (ctx) => {
251
  }
252
  })
253
 
254
- // 注册路由中间件
255
  app.use(router.routes()).use(router.allowedMethods())
256
 
257
- // 错误处理中间件之前添加请求日志中间件
258
- app.use(async (ctx, next) => {
259
- const start = Date.now()
260
- console.log(`[${new Date().toISOString()}] ${ctx.method} ${ctx.url} - Request started`)
261
- await next()
262
- const ms = Date.now() - start
263
- console.log(`[${new Date().toISOString()}] ${ctx.method} ${ctx.url} - Response ${ctx.status} - ${ms}ms`)
264
- })
265
-
266
- // 修改错误处理中间件
267
- app.use(async (ctx, next) => {
268
- try {
269
- await next()
270
- // 处理 404 错误
271
- if (ctx.status === 404) {
272
- ctx.status = 404
273
- ctx.body = {
274
- error: 'Not Found',
275
- message: `The requested path ${ctx.path} was not found`,
276
- availableEndpoints: [
277
- '/v1/chat/completions',
278
- '/hf/v1/chat/completions',
279
- '/v1/models',
280
- '/hf/v1/models'
281
- ]
282
- }
283
- }
284
- } catch (err) {
285
- console.error('Error details:', {
286
- path: ctx.path,
287
- method: ctx.method,
288
- error: err.message,
289
- stack: err.stack
290
- })
291
- ctx.status = err.status || 500
292
- ctx.body = {
293
- error: err.name || 'Internal Server Error',
294
- message: err.message,
295
- path: ctx.path
296
- }
297
- ctx.app.emit('error', err, ctx)
298
- }
299
- })
300
-
301
  // 启动服务器
302
  const PORT = process.env.PORT || 3000
303
  app.listen(PORT, '0.0.0.0', () => {
 
19
  textLimit: '30mb', // text 数据大小限制
20
  }))
21
 
22
+ // 添加 CORS 中间件
23
+ app.use(async (ctx, next) => {
24
+ ctx.set('Access-Control-Allow-Origin', '*');
25
+ ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
26
+ ctx.set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
27
+ ctx.set('Access-Control-Max-Age', '3600');
28
+
29
+ // 处理 OPTIONS 请求
30
+ if (ctx.method === 'OPTIONS') {
31
+ ctx.status = 204;
32
+ return;
33
+ }
34
+
35
+ await next();
36
+ });
37
+
38
+ // 添加请求日志中间件
39
+ app.use(async (ctx, next) => {
40
+ const start = Date.now()
41
+ console.log(`[${new Date().toISOString()}] ${ctx.method} ${ctx.url} - Request started`)
42
+ try {
43
+ await next()
44
+ } catch (err) {
45
+ throw err
46
+ } finally {
47
+ const ms = Date.now() - start
48
+ console.log(`[${new Date().toISOString()}] ${ctx.method} ${ctx.url} - Response ${ctx.status} - ${ms}ms`)
49
+ }
50
+ })
51
+
52
+ // 错误处理中间件
53
+ app.use(async (ctx, next) => {
54
+ try {
55
+ await next()
56
+ // 处理 404 错误
57
+ if (ctx.status === 404) {
58
+ ctx.status = 404
59
+ ctx.body = {
60
+ error: 'Not Found',
61
+ message: `The requested path ${ctx.path} was not found`,
62
+ availableEndpoints: [
63
+ '/v1/chat/completions',
64
+ '/hf/v1/chat/completions',
65
+ '/v1/models',
66
+ '/hf/v1/models'
67
+ ]
68
+ }
69
+ }
70
+ } catch (err) {
71
+ console.error('Error details:', {
72
+ path: ctx.path,
73
+ method: ctx.method,
74
+ error: err.message,
75
+ stack: err.stack
76
+ })
77
+ ctx.status = err.status || 500
78
+ ctx.body = {
79
+ error: err.name || 'Internal Server Error',
80
+ message: err.message,
81
+ path: ctx.path
82
+ }
83
+ ctx.app.emit('error', err, ctx)
84
+ }
85
+ })
86
+
87
  const makeRequest = async (session_id, requestModel, messages) => {
88
  console.log('开始请求 genspark.ai:', { session_id, requestModel })
89
  try {
 
282
  }
283
  }
284
 
285
+ // 创建获取模型列表处理函数
286
  const handleModels = async (ctx) => {
287
  ctx.body = {
288
  object: "list",
 
316
  }
317
  })
318
 
319
+ // 注册路由��间件 (移到错误处理之后)
320
  app.use(router.routes()).use(router.allowedMethods())
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  // 启动服务器
323
  const PORT = process.env.PORT || 3000
324
  app.listen(PORT, '0.0.0.0', () => {