From 0545b4ffcd9459287bf031c35dbef92744128e24 Mon Sep 17 00:00:00 2001 From: dongzp_book <90fanhua@gmail.com> Date: Tue, 7 Apr 2026 23:42:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=9F=A5=E8=AF=A2=5F=5Fconfi?= =?UTF-8?q?g=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.api.ts | 151 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 63 deletions(-) diff --git a/index.api.ts b/index.api.ts index 2ac0c1f..28d3357 100644 --- a/index.api.ts +++ b/index.api.ts @@ -166,6 +166,94 @@ const proxyServer = http.createServer((clientReq, clientRes) => { const parsedUrl = new URL(`http://localhost${clientReq.url!}`); const requestPath = parsedUrl.pathname; + // 管理接口:保留路径,不参与代理转发 + if (requestPath === "/__reload-config") { + if (clientReq.method !== "POST") { + clientRes.writeHead(405, { + "Content-Type": "application/json", + Allow: "POST", + }); + clientRes.end( + JSON.stringify({ + success: false, + error: "Method Not Allowed", + allow: ["POST"], + }), + ); + return; + } + + try { + const oldCount = Object.keys(MOCK_ROUTES).length; + loadConfig(); + const newCount = Object.keys(MOCK_ROUTES).length; + + clientRes.writeHead(200, { "Content-Type": "application/json" }); + clientRes.end( + JSON.stringify({ + success: true, + message: "Configuration reloaded successfully", + routesCount: newCount, + routesChanged: newCount - oldCount, + }), + ); + console.log( + `[ADMIN] 通过API重新加载配置 (路由数: ${oldCount} -> ${newCount})`, + ); + } catch (error) { + clientRes.writeHead(500, { "Content-Type": "application/json" }); + clientRes.end( + JSON.stringify({ + success: false, + error: (error as Error).message, + }), + ); + } + return; + } + + if (requestPath === "/__config") { + if (clientReq.method !== "GET") { + clientRes.writeHead(405, { + "Content-Type": "application/json", + Allow: "GET", + }); + clientRes.end( + JSON.stringify({ + success: false, + error: "Method Not Allowed", + allow: ["GET"], + }), + ); + return; + } + + try { + clientRes.writeHead(200, { "Content-Type": "application/json" }); + clientRes.end( + JSON.stringify( + { + routes: MOCK_ROUTES, + config: CONFIG, + timestamp: new Date().toISOString(), + totalRoutes: Object.keys(MOCK_ROUTES).length, + }, + null, + 2, + ), + ); + } catch (error) { + clientRes.writeHead(500, { "Content-Type": "application/json" }); + clientRes.end( + JSON.stringify({ + success: false, + error: (error as Error).message, + }), + ); + } + return; + } + // 检查是否为需要mock的路由 if (isMockRoute(requestPath)) { const mockFile = getMockFilePath(requestPath); @@ -265,69 +353,6 @@ const proxyServer = http.createServer((clientReq, clientRes) => { clientReq.pipe(proxyReq); }); -// 添加管理接口用于重新加载配置 -proxyServer.on("request", (req, res) => { - const parsedUrl = new URL(`http://localhost${req.url!}`); - const pathname = parsedUrl.pathname; - - // 管理接口:重新加载配置 - if (pathname === "/__reload-config" && req.method === "POST") { - try { - const oldCount = Object.keys(MOCK_ROUTES).length; - loadConfig(); - const newCount = Object.keys(MOCK_ROUTES).length; - - res.writeHead(200, { "Content-Type": "application/json" }); - res.end( - JSON.stringify({ - success: true, - message: "Configuration reloaded successfully", - routesCount: newCount, - routesChanged: newCount - oldCount, - }), - ); - console.log( - `[ADMIN] 通过API重新加载配置 (路由数: ${oldCount} -> ${newCount})`, - ); - } catch (error) { - res.writeHead(500, { "Content-Type": "application/json" }); - res.end( - JSON.stringify({ - success: false, - error: (error as Error).message, - }), - ); - } - } - - // 管理接口:查看当前配置 - if (pathname === "/__config" && req.method === "GET") { - try { - res.writeHead(200, { "Content-Type": "application/json" }); - res.end( - JSON.stringify( - { - routes: MOCK_ROUTES, - config: CONFIG, - timestamp: new Date().toISOString(), - totalRoutes: Object.keys(MOCK_ROUTES).length, - }, - null, - 2, - ), - ); - } catch (error) { - res.writeHead(500, { "Content-Type": "application/json" }); - res.end( - JSON.stringify({ - success: false, - error: (error as Error).message, - }), - ); - } - } -}); - // 初始化:第一次加载配置 loadConfig();