PagerMaid-Pyro/pagermaid/web/api/status.py

56 lines
1.6 KiB
Python
Raw Normal View History

2023-01-31 16:24:56 +00:00
import asyncio
from typing import Union
2023-01-31 16:24:56 +00:00
from fastapi import APIRouter
2023-01-31 16:24:56 +00:00
from fastapi.responses import JSONResponse, StreamingResponse
from pagermaid.common.status import get_status
from pagermaid.common.system import run_eval
from pagermaid.utils import execute
from pagermaid.web.api.utils import authentication
route = APIRouter()
@route.get("/log", dependencies=[authentication()])
async def get_log(num: Union[int, str] = 100):
2023-01-31 16:24:56 +00:00
try:
num = int(num)
except ValueError:
num = 100
async def streaming_logs():
with open("data/pagermaid.log.txt", "r", encoding="utf-8") as f:
2023-01-31 16:24:56 +00:00
for log in f.readlines()[-num:]:
yield log
await asyncio.sleep(0.02)
return StreamingResponse(streaming_logs())
@route.get("/run_eval", dependencies=[authentication()])
async def run_cmd(cmd: str = ""):
2023-01-31 16:24:56 +00:00
async def run_cmd_func():
2024-02-05 13:16:25 +00:00
result = (await run_eval(cmd)).split("\n")
2023-01-31 16:24:56 +00:00
for i in result:
yield i + "\n"
await asyncio.sleep(0.02)
return StreamingResponse(run_cmd_func()) if cmd else "无效命令"
@route.get("/run_sh", dependencies=[authentication()])
async def run_sh(cmd: str = ""):
2023-01-31 16:24:56 +00:00
async def run_sh_func():
result = (await execute(cmd)).split("\n")
for i in result:
yield i + "\n"
await asyncio.sleep(0.02)
return StreamingResponse(run_sh_func()) if cmd else "无效命令"
2023-03-12 03:56:01 +00:00
@route.get("/status", response_class=JSONResponse, dependencies=[authentication()])
2023-01-31 16:24:56 +00:00
async def status():
return (await get_status()).dict()