refactor(api): 将健康检查和令牌计数处理逻辑移至请求处理器

重构代码结构,将原本在api-manager.js中的健康检查端点和令牌计数请求处理
This commit is contained in:
hex2077 2025-11-14 15:10:58 +08:00
parent 0149bf6558
commit 1dde8b5a74
2 changed files with 22 additions and 20 deletions

View file

@ -18,27 +18,7 @@ import {
* @returns {Promise<boolean>} - True if the request was handled by API
*/
export async function handleAPIRequests(method, path, req, res, currentConfig, apiService, providerPoolManager, promptLogFilename) {
// Health check endpoint
if (method === 'GET' && path === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
timestamp: new Date().toISOString(),
provider: currentConfig.MODEL_PROVIDER
}));
return true;
}
// Ignore count_tokens requests
if (path.includes('/count_tokens')) {
console.log(`[Server] Ignoring count_tokens request: ${path}`);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
tokens: 0,
message: 'Token counting is not supported'
}));
return true;
}
// Route model list requests
if (method === 'GET') {

View file

@ -80,6 +80,28 @@ export function createRequestHandler(config, providerPoolManager) {
}
return;
}
// Health check endpoint
if (method === 'GET' && path === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
timestamp: new Date().toISOString(),
provider: currentConfig.MODEL_PROVIDER
}));
return true;
}
// Ignore count_tokens requests
if (path.includes('/count_tokens')) {
console.log(`[Server] Ignoring count_tokens request: ${path}`);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
tokens: 0,
message: 'Token counting is not supported'
}));
return true;
}
// Check authentication for API requests
if (!isAuthorized(req, requestUrl, currentConfig.REQUIRED_API_KEY)) {