Upload 2 files
Browse files- src/index.js +49 -4
src/index.js
CHANGED
|
@@ -217,7 +217,7 @@ const handleChatCompletions = async (ctx) => {
|
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
-
//
|
| 221 |
const handleModels = async (ctx) => {
|
| 222 |
ctx.body = {
|
| 223 |
object: "list",
|
|
@@ -236,18 +236,63 @@ router.post('/hf/v1/chat/completions', handleChatCompletions)
|
|
| 236 |
router.get('/v1/models', handleModels)
|
| 237 |
router.get('/hf/v1/models', handleModels)
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
// 注册路由中间件
|
| 240 |
app.use(router.routes()).use(router.allowedMethods())
|
| 241 |
|
| 242 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
app.use(async (ctx, next) => {
|
| 244 |
try {
|
| 245 |
await next()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
} catch (err) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
ctx.status = err.status || 500
|
| 248 |
ctx.body = {
|
| 249 |
-
|
| 250 |
-
message: err.message
|
|
|
|
| 251 |
}
|
| 252 |
ctx.app.emit('error', err, ctx)
|
| 253 |
}
|
|
|
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
+
// 创建获取模型列表��处理函数
|
| 221 |
const handleModels = async (ctx) => {
|
| 222 |
ctx.body = {
|
| 223 |
object: "list",
|
|
|
|
| 236 |
router.get('/v1/models', handleModels)
|
| 237 |
router.get('/hf/v1/models', handleModels)
|
| 238 |
|
| 239 |
+
// 添加根路由处理器
|
| 240 |
+
router.get('/', async (ctx) => {
|
| 241 |
+
ctx.body = {
|
| 242 |
+
status: 'ok',
|
| 243 |
+
message: 'API server is running',
|
| 244 |
+
version: '1.0.0',
|
| 245 |
+
endpoints: [
|
| 246 |
+
'/v1/chat/completions',
|
| 247 |
+
'/hf/v1/chat/completions',
|
| 248 |
+
'/v1/models',
|
| 249 |
+
'/hf/v1/models'
|
| 250 |
+
]
|
| 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 |
}
|