File size: 847 Bytes
bc0be9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// router.ts
import { handleMeow, handleModels, handleChatCompletions } from "./handlers.ts";
import { errorResponse } from "./response.ts";


export async function router(req: Request): Promise<Response> {
  const { method } = req;
  const url = new URL(req.url);
  const pathname = url.pathname;
  
  const routes = [
    { method: "GET", path: "/v1/models", handler: handleModels },
    { method: "POST", path: "/v1/chat/completions", handler: handleChatCompletions },
    { method: "GET", path: "/", handler: handleMeow },
  ];
  
  const route = routes.find(r => r.method === method && r.path === pathname);
  
  if (route) {
    try {
      return await route.handler(req);
    } catch (error) {
      return errorResponse(`处理请求时发生错误: ${error.message}`, 500);
    }
  }
  
  return errorResponse("未找到路由", 404);
}