修复日志获取

This commit is contained in:
whyour 2022-05-24 10:00:42 +08:00
parent af74afd10d
commit 76b6677f5a
4 changed files with 36 additions and 85 deletions

View File

@ -3,7 +3,8 @@ import { Container } from 'typedi';
import { Logger } from 'winston'; import { Logger } from 'winston';
import * as fs from 'fs'; import * as fs from 'fs';
import config from '../config'; import config from '../config';
import { getFileContentByName } from '../config/util'; import { getFileContentByName, readDirs } from '../config/util';
import { join } from 'path';
const route = Router(); const route = Router();
export default (app: Router) => { export default (app: Router) => {
@ -12,59 +13,28 @@ export default (app: Router) => {
route.get('/', async (req: Request, res: Response, next: NextFunction) => { route.get('/', async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const fileList = fs.readdirSync(config.logPath, 'utf-8'); const result = readDirs(config.logPath, config.logPath);
const dirs = []; res.send({
for (let i = 0; i < fileList.length; i++) { code: 200,
const stat = fs.lstatSync(config.logPath + fileList[i]); data: result,
if (stat.isDirectory()) { });
const fileListTmp = fs.readdirSync(
`${config.logPath}/${fileList[i]}`,
'utf-8',
);
dirs.push({
name: fileList[i],
isDir: true,
files: fileListTmp.reverse(),
});
} else {
dirs.push({
name: fileList[i],
isDir: false,
files: [],
});
}
}
res.send({ code: 200, dirs });
} catch (e) { } catch (e) {
logger.error('🔥 error: %o', e); logger.error('🔥 error: %o', e);
return next(e); return next(e);
} }
}); });
route.get(
'/:dir/:file',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const { dir, file } = req.params;
const content = getFileContentByName(
`${config.logPath}/${dir}/${file}`,
);
res.send({ code: 200, data: content });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.get( route.get(
'/:file', '/:file',
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const { file } = req.params; const filePath = join(
const content = getFileContentByName(`${config.logPath}/${file}`); config.logPath,
req.query.path as string,
req.params.file,
);
const content = getFileContentByName(filePath);
res.send({ code: 200, data: content }); res.send({ code: 200, data: content });
} catch (e) { } catch (e) {
logger.error('🔥 error: %o', e); logger.error('🔥 error: %o', e);

View File

@ -17,22 +17,19 @@ const route = Router();
export default (app: Router) => { export default (app: Router) => {
app.use('/scripts', route); app.use('/scripts', route);
route.get( route.get('/', async (req: Request, res: Response, next: NextFunction) => {
'/files', const logger: Logger = Container.get('logger');
async (req: Request, res: Response, next: NextFunction) => { try {
const logger: Logger = Container.get('logger'); const result = readDirs(config.scriptPath, config.scriptPath);
try { res.send({
const result = readDirs(config.scriptPath, config.scriptPath); code: 200,
res.send({ data: result,
code: 200, });
data: result, } catch (e) {
}); logger.error('🔥 error: %o', e);
} catch (e) { return next(e);
logger.error('🔥 error: %o', e); }
return next(e); });
}
},
);
route.get( route.get(
'/:file', '/:file',

View File

@ -52,34 +52,18 @@ const Log = ({ headerStyle, isPhone, theme }: any) => {
request request
.get(`${config.apiPrefix}logs`) .get(`${config.apiPrefix}logs`)
.then((data) => { .then((data) => {
const result = formatData(data.dirs) as any; setData(data.data);
setData(result); setFilterData(data.data);
setFilterData(result);
}) })
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}; };
const formatData = (tree: any[]) => {
return tree.map((x) => {
x.title = x.name;
x.value = x.name;
x.disabled = x.isDir;
x.key = x.name;
x.children = x.files.map((y: string) => ({
title: y,
value: `${x.name}/${y}`,
key: `${x.name}/${y}`,
parent: x.name,
isLeaf: true,
}));
return x;
});
};
const getLog = (node: any) => { const getLog = (node: any) => {
request.get(`${config.apiPrefix}logs/${node.value}`).then((data) => { request
setValue(data.data); .get(`${config.apiPrefix}logs/${node.value}?path=${node.parent || ''}`)
}); .then((data) => {
setValue(data.data);
});
}; };
const onSelect = (value: any, node: any) => { const onSelect = (value: any, node: any) => {
@ -88,7 +72,7 @@ const Log = ({ headerStyle, isPhone, theme }: any) => {
} }
setValue('加载中...'); setValue('加载中...');
setSelect(value); setSelect(value);
setTitle(node.parent || node.value); setTitle(node.key);
getLog(node); getLog(node);
}; };

View File

@ -93,7 +93,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const getScripts = () => { const getScripts = () => {
setLoading(true); setLoading(true);
request request
.get(`${config.apiPrefix}scripts/files`) .get(`${config.apiPrefix}scripts`)
.then((data) => { .then((data) => {
setData(data.data); setData(data.data);
setFilterData(data.data); setFilterData(data.data);
@ -135,7 +135,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const newMode = value ? LangMap[value.slice(-3)] : ''; const newMode = value ? LangMap[value.slice(-3)] : '';
setMode(isPhone && newMode === 'typescript' ? 'javascript' : newMode); setMode(isPhone && newMode === 'typescript' ? 'javascript' : newMode);
setSelect(node.key); setSelect(node.key);
setTitle(node.parent || node.value); setTitle(node.key);
setCurrentNode(node); setCurrentNode(node);
getDetail(node); getDetail(node);
}; };