主文件功能拆分

This commit is contained in:
2025-09-26 15:31:29 +08:00
parent a9dcbc2578
commit efb68fce1c
8 changed files with 284 additions and 172 deletions
+13
View File
@@ -0,0 +1,13 @@
/**
* 后端实例注入中间件
* 将PixivBackend实例注入到请求对象中,供后续中间件和路由处理器使用
*/
function backendInjector(backend) {
return (req, res, next) => {
req.backend = backend;
next();
};
}
module.exports = { backendInjector };
+16
View File
@@ -0,0 +1,16 @@
/**
* Body Parser中间件
* 处理JSON和URL编码的请求体
*/
const express = require('express');
function bodyParserMiddleware() {
return [
// JSON 解析中间件
express.json({ limit: '20mb' }),
// URL编码解析中间件
express.urlencoded({ extended: true, limit: '20mb' })
];
}
module.exports = { bodyParserMiddleware };
+13
View File
@@ -0,0 +1,13 @@
/**
* CORS中间件配置
*/
const cors = require('cors');
function corsMiddleware() {
return cors({
origin: process.env.FRONTEND_URL || true, // 允许所有来源,或者通过环境变量指定
credentials: true,
});
}
module.exports = { corsMiddleware };
+106
View File
@@ -0,0 +1,106 @@
/**
* 日志中间件
*/
const { defaultLogger } = require('../utils/logger');
// 创建logger实例
const logger = defaultLogger.child('API');
/**
* 自定义日志中间件
*/
function loggerMiddleware(req, res, next) {
// 过滤掉静态资源请求和图片代理请求
const isStaticResource =
req.path.startsWith('/assets/') ||
req.path.startsWith('/downloads/') ||
req.path.includes('.js') ||
req.path.includes('.css') ||
req.path.includes('.ico') ||
req.path.includes('.png') ||
req.path.includes('.jpg') ||
req.path.includes('.jpeg') ||
req.path.includes('.gif') ||
req.path.includes('.svg') ||
req.path.includes('.woff') ||
req.path.includes('.woff2') ||
req.path.includes('.ttf') ||
req.path.includes('.eot');
// 过滤掉图片代理请求
const isImageProxy = req.path === '/api/proxy/image';
// 过滤掉下载任务状态查询请求
const isDownloadTasksQuery =
req.path === '/api/download/tasks' ||
req.path === '/api/download/tasks/active' ||
req.path === '/api/download/tasks/summary' ||
req.path === '/api/download/tasks/changes' ||
req.path === '/api/download/tasks/completed';
// 过滤掉仓库预览请求(图片预览)
const isRepositoryPreview = req.path === '/api/repository/preview';
// 过滤掉健康检查请求
const isHealthCheck = req.path === '/health';
// 只记录重要的API请求,排除静态资源、图片代理、下载任务查询、仓库预览和健康检查
if (!isStaticResource && !isImageProxy && !isDownloadTasksQuery && !isRepositoryPreview && !isHealthCheck) {
const start = Date.now();
// 原始响应结束方法
const originalEnd = res.end;
// 重写响应结束方法以获取响应时间
res.end = function (chunk, encoding) {
const duration = Date.now() - start;
const statusCode = res.statusCode;
const method = req.method;
const url = req.originalUrl;
// 根据状态码选择图标
let statusIcon;
if (statusCode >= 200 && statusCode < 300) {
statusIcon = '✅';
} else if (statusCode >= 300 && statusCode < 400) {
statusIcon = '🔄';
} else if (statusCode >= 400 && statusCode < 500) {
statusIcon = '⚠️';
} else {
statusIcon = '❌';
}
// 根据请求类型选择图标
let methodIcon;
switch (method) {
case 'GET':
methodIcon = '📥';
break;
case 'POST':
methodIcon = '📤';
break;
case 'PUT':
methodIcon = '🔄';
break;
case 'DELETE':
methodIcon = '🗑️';
break;
case 'PATCH':
methodIcon = '🔧';
break;
default:
methodIcon = '❓';
}
// 输出日志
logger.info(`${statusIcon} ${methodIcon} ${method} ${url} ${statusCode} ${duration}ms`);
// 调用原始的end方法
originalEnd.call(this, chunk, encoding);
};
}
next();
}
module.exports = { loggerMiddleware };
+17
View File
@@ -0,0 +1,17 @@
/**
* 静态文件服务中间件
*/
const express = require('express');
const path = require('path');
function staticFilesMiddleware() {
return [
// 下载文件静态服务
express.static(path.join(__dirname, '../../downloads')),
// 前端静态文件服务
express.static(path.join(__dirname, '../../ui/dist'))
];
}
module.exports = { staticFilesMiddleware };