完善基础框架,对原有APILoader进行接口响应的错误处理

This commit is contained in:
tone
2024-09-25 01:21:23 +08:00
parent a9c67cd013
commit 7acdfc2c68
12 changed files with 398 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import { Request, Response, NextFunction } from "express"
import Logger from "@lib/Logger/Logger";
const logger = new Logger('API', 'Middleware', 'MountIP');
let MountIP = (req: Request, res: Response, next: NextFunction) => {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.ip;
if (ip == undefined || ip.length <= 0) {
logger.warn(`[${req.method}][${req.url.split('?')[0]}] ip resolution was fail`);
} else {
if (typeof ip === 'object')
req.body._ip = ip.join(' ').replace('::ffff:', '');
else
req.body._ip = ip.replace('::ffff:', '');
logger.info(`[${req.method}][${req.url.split('?')[0]}] ip resolution was successful: ${req.body._ip}`);
}
next();
}
export default MountIP;
interface MountIPRequestData {
_ip: string,
[key: string | number | symbol]: any
}
export type { MountIPRequestData };