mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-22 22:36:06 +08:00
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { Router, Request, Response, NextFunction } from 'express';
|
|
import { Container } from 'typedi';
|
|
import { Logger } from 'winston';
|
|
import * as fs from 'fs';
|
|
import config from '../config';
|
|
import { getFileContentByName, readDirs } from '../config/util';
|
|
import { join } from 'path';
|
|
const route = Router();
|
|
|
|
export default (app: Router) => {
|
|
app.use('/logs', route);
|
|
|
|
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
|
const logger: Logger = Container.get('logger');
|
|
try {
|
|
const result = readDirs(config.logPath, config.logPath);
|
|
res.send({
|
|
code: 200,
|
|
data: result,
|
|
});
|
|
} catch (e) {
|
|
logger.error('🔥 error: %o', e);
|
|
return next(e);
|
|
}
|
|
});
|
|
|
|
route.get(
|
|
'/:file',
|
|
async (req: Request, res: Response, next: NextFunction) => {
|
|
const logger: Logger = Container.get('logger');
|
|
try {
|
|
const filePath = join(
|
|
config.logPath,
|
|
(req.query.path || '') as string,
|
|
req.params.file,
|
|
);
|
|
const content = getFileContentByName(filePath);
|
|
res.send({ code: 200, data: content });
|
|
} catch (e) {
|
|
return next(e);
|
|
}
|
|
},
|
|
);
|
|
};
|