From c0f09ec5bb69f5cbc22cfc271c80b3ac76fd7d9a Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 18 Mar 2021 23:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0log=E6=9F=A5=E8=AF=A2api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + back/api/index.ts | 2 + back/api/log.ts | 77 +++++++++++++++++++++++++++++++++++++ src/pages/code/index.tsx | 1 - src/pages/config/index.tsx | 58 ---------------------------- src/pages/crontab/index.tsx | 1 - src/pages/diy/index.tsx | 1 - src/pages/log/index.tsx | 35 +++++++++++------ 8 files changed, 103 insertions(+), 73 deletions(-) create mode 100644 back/api/log.ts diff --git a/.gitignore b/.gitignore index 01598be8..042e6e73 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ /src/.umi-test /.env.local /config +/log .env \ No newline at end of file diff --git a/back/api/index.ts b/back/api/index.ts index dd93bf02..e6320a80 100644 --- a/back/api/index.ts +++ b/back/api/index.ts @@ -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; }; diff --git a/back/api/log.ts b/back/api/log.ts new file mode 100644 index 00000000..6d9feb8b --- /dev/null +++ b/back/api/log.ts @@ -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); + } + }, + ); +}; diff --git a/src/pages/code/index.tsx b/src/pages/code/index.tsx index c33988fd..d91c76c2 100644 --- a/src/pages/code/index.tsx +++ b/src/pages/code/index.tsx @@ -39,7 +39,6 @@ const Crontab = () => { { }); }; - 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: ( -
- -
- ), - }); - 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:
{cookie}
, - }); - 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 = () => { 保存 diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index c8b53364..9ba91e6e 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -51,7 +51,6 @@ const Crontab = () => { 保存 diff --git a/src/pages/diy/index.tsx b/src/pages/diy/index.tsx index 49976d1e..8c66a9f8 100644 --- a/src/pages/diy/index.tsx +++ b/src/pages/diy/index.tsx @@ -51,7 +51,6 @@ const Crontab = () => { 保存 diff --git a/src/pages/log/index.tsx b/src/pages/log/index.tsx index 0886e3aa..5b426087 100644 --- a/src/pages/log/index.tsx +++ b/src/pages/log/index.tsx @@ -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 = () => { { showSearch key="title" onSelect={onSelect} - /> + />, ]} header={{ style: {