完善基础框架,对原有APILoader进行接口响应的错误处理
This commit is contained in:
27
src/lib/APIMiddleware/MountIP.ts
Normal file
27
src/lib/APIMiddleware/MountIP.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user