添加log查询api

This commit is contained in:
whyour 2021-03-18 23:51:35 +08:00
parent b2c22327f9
commit c0f09ec5bb
8 changed files with 103 additions and 73 deletions

1
.gitignore vendored
View File

@ -20,4 +20,5 @@
/src/.umi-test
/.env.local
/config
/log
.env

View File

@ -2,11 +2,13 @@ import { Router } from 'express';
import auth from './auth';
import cookie from './cookie';
import config from './config';
import log from './log';
export default () => {
const app = Router();
auth(app);
cookie(app);
config(app);
log(app);
return app;
};

77
back/api/log.ts Normal file
View File

@ -0,0 +1,77 @@
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 } from '../config/util';
const route = Router();
export default (app: Router) => {
app.use('/', route);
route.get(
'/logs',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const fileList = fs.readdirSync(config.logPath, 'utf-8');
const dirs = [];
for (let i = 0; i < fileList.length; i++) {
const stat = fs.lstatSync(config.logPath + fileList[i]);
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) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.get(
'/logs/: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(
'/logs/:file',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const { file } = req.params;
const content = getFileContentByName(`${config.logPath}/${file}`);
res.send({ code: 200, data: content });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
};

View File

@ -39,7 +39,6 @@ const Crontab = () => {
<PageContainer
className="code-mirror-wrapper"
title="互助码"
loading={loading}
header={{
style: {
padding: '4px 16px 4px 15px',

View File

@ -35,63 +35,6 @@ const Config = () => {
});
};
function sleep(time: number) {
return new Promise((resolve) => setTimeout(resolve, time));
}
const showQrCode = () => {
request.get(`${config.apiPrefix}qrcode`).then(async (data) => {
const modal = Modal.info({
title: '二维码',
content: (
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
marginLeft: -38,
}}
>
<QRCode
style={{
width: 200,
height: 200,
marginBottom: 10,
marginTop: 20,
}}
value={data.qrcode}
/>
</div>
),
});
getCookie(modal);
});
};
const getCookie = async (modal: { destroy: () => void }) => {
for (let i = 0; i < 50; i++) {
const {
data: { cookie, errcode, message },
} = await request.get(`${config.apiPrefix}cookie`);
if (cookie) {
notification.success({
message: 'Cookie获取成功',
});
modal.destroy();
Modal.success({
title: '获取Cookie成功',
content: <div>{cookie}</div>,
});
break;
}
if (errcode !== 176) {
notification.error({ message });
break;
}
await sleep(2000);
}
};
useEffect(() => {
if (document.body.clientWidth < 768) {
setWdith('auto');
@ -109,7 +52,6 @@ const Config = () => {
<PageContainer
className="code-mirror-wrapper"
title="config.sh"
loading={loading}
extra={[
<Button key="1" type="primary" onClick={updateConfig}>

View File

@ -51,7 +51,6 @@ const Crontab = () => {
<PageContainer
className="code-mirror-wrapper"
title="crontab.list"
loading={loading}
extra={[
<Button key="1" type="primary" onClick={updateConfig}>

View File

@ -51,7 +51,6 @@ const Crontab = () => {
<PageContainer
className="code-mirror-wrapper"
title="diy.sh"
loading={loading}
extra={[
<Button key="1" type="primary" onClick={updateConfig}>

View File

@ -21,20 +21,32 @@ const Log = () => {
};
const formatData = (tree: any[]) => {
return tree.map(x => {
x.title = x.dirName;
x.value = x.dirName;
x.disabled = true;
x.children = x.files.map((y: string) => ({ title: y, key: y, value: y, parent: x.dirName }));
return tree.map((x) => {
x.title = x.name;
x.value = x.name;
x.disabled = x.isDir;
x.children = x.files.map((y: string) => ({
title: y,
key: y,
value: y,
parent: x.name,
}));
return x;
})
}
});
};
const getLog = (node: any) => {
setLoading(true);
request.get(`${config.apiPrefix}logs/${node.parent}/${node.value}`).then((data) => {
setValue(data);
}).finally(() => setLoading(false));
let url = `${node.parent}/${node.value}`;
if (!node.isDir) {
url = node.value;
}
request
.get(`${config.apiPrefix}logs/${url}`)
.then((data) => {
setValue(data.data);
})
.finally(() => setLoading(false));
};
const onSelect = (value: any, node: any) => {
@ -60,7 +72,6 @@ const Log = () => {
<PageContainer
className="code-mirror-wrapper"
title={title}
loading={loading}
extra={[
<TreeSelect
style={{ width: 280 }}
@ -71,7 +82,7 @@ const Log = () => {
showSearch
key="title"
onSelect={onSelect}
/>
/>,
]}
header={{
style: {