From fa286d3bbadd23c81d392766cb5a1a061ef588fa Mon Sep 17 00:00:00 2001 From: CMHopeSunshine <277073121@qq.com> Date: Thu, 17 Nov 2022 16:25:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20`Web=20UI`=E5=A2=9E=E5=8A=A0`?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=91=BD=E4=BB=A4`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LittlePaimon/web/api/status.py | 8 ++++++- LittlePaimon/web/pages/home_page.py | 33 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/LittlePaimon/web/api/status.py b/LittlePaimon/web/api/status.py index f7a10bc..b8c77a8 100644 --- a/LittlePaimon/web/api/status.py +++ b/LittlePaimon/web/api/status.py @@ -31,7 +31,7 @@ logger.add(record_debug_log, level='DEBUG', colorize=True, filter=default_filter route = APIRouter() -@route.get('/log', response_class=StreamingResponse) +@route.get('/log', response_class=StreamingResponse, dependencies=[authentication()]) async def get_log(level: str = 'info', num: Union[int, str] = 100): show_logs = info_logs[-(num or 1):] if level == 'info' else debug_logs[-(num or 1):] @@ -43,6 +43,12 @@ async def get_log(level: str = 'info', num: Union[int, str] = 100): return StreamingResponse(streaming_logs()) +@route.get('/run_cmd', response_class=StreamingResponse, dependencies=[authentication()]) +async def run_cmd(cmd: str): + p = await asyncio.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) + return StreamingResponse(p.stdout or p.stderr) + + @route.get('/status', response_class=JSONResponse, dependencies=[authentication()]) async def status(): return await get_status() diff --git a/LittlePaimon/web/pages/home_page.py b/LittlePaimon/web/pages/home_page.py index c93500b..1cdc21d 100644 --- a/LittlePaimon/web/pages/home_page.py +++ b/LittlePaimon/web/pages/home_page.py @@ -1,5 +1,6 @@ from LittlePaimon.utils import __version__ -from amis import Page, PageSchema, Html, Property, Service, Flex, ActionType, LevelEnum, Divider, ButtonGroupSelect, Log, Alert, Form, Dialog, Select, Group +from amis import Page, PageSchema, Html, Property, Service, Flex, ActionType, LevelEnum, Divider, ButtonGroupSelect, \ + Log, Alert, Form, Dialog, Select, Group, InputText, DisplayModeEnum, Horizontal logo = Html(html=f'''

@@ -70,6 +71,27 @@ log_page = Log( source='/LittlePaimon/api/log?level=${log_level | raw}&num=${log_num | raw}' ) +cmd_input = Form( + mode=DisplayModeEnum.horizontal, + horizontal=Horizontal(left=0), + wrapWithPanel=False, + body=[ + InputText(name='command', required=True, clearable=True, addOn=ActionType.Dialog( + label='执行', + level=LevelEnum.primary, + dialog=Dialog( + title='命令执行结果', + size='xl', + body=Log( + autoScroll=True, + placeholder='执行命令中,请稍候...', + operation=['stop', 'showLineNumber', 'filter'], + source='/LittlePaimon/api/run_cmd?cmd=${command | raw}'), + ) + )) + ] +) + operation_button = Flex(justify='center', items=[ ActionType.Ajax( label='更新', @@ -97,6 +119,15 @@ operation_button = Flex(justify='center', items=[ Form( body=[Group(body=[select_log_num, select_log_level]), log_page] )]) + ), + ActionType.Dialog( + label='执行命令', + className='m-l', + level=LevelEnum.warning, + dialog=Dialog(title='执行命令', + size='lg', + actions=[], + body=[cmd_input]) ) ])