Files
zTools2/app/controllers/system_controller.py
zikai 80b96d236f init: 从 /root/zikai 根目录迁入
把原本散落在 /root/zikai 的 FastAPI 服务整理到 server/ 子目录,
开启独立 git 与 venv。运行时数据(config.yaml / keys / uploads)
按 .gitignore 留在工作目录但不入仓。
2026-06-23 16:17:31 +00:00

40 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""系统监控接口。"""
from __future__ import annotations
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, JSONResponse, Response
from ..schemas.system import SystemStatus
from ..services.system_service import SystemService
from ..views.system_status_html import render as render_status_html
router = APIRouter(prefix="/api/system", tags=["system"])
def _wants_html(request: Request, fmt: str | None) -> bool:
if fmt == "json":
return False
if fmt == "html":
return True
accept = request.headers.get("accept", "").lower()
return "text/html" in accept and "application/json" not in accept
@router.get(
"/status",
summary="主机资源状态HTML 或 JSON",
description=(
"返回 CPU、内存、磁盘的实时使用情况数据来源 psutil\n\n"
"**内容协商**:浏览器(`Accept: text/html`)返回 HTML 页面API 客户端返回 JSON。"
"可用 `?format=html` / `?format=json` 强制指定。"
),
response_model=SystemStatus,
responses={200: {"content": {"application/json": {}, "text/html": {"schema": {"type": "string"}}}}},
)
def get_status(request: Request, format: str | None = None) -> Response:
status = SystemService().get_status()
if _wants_html(request, format):
return HTMLResponse(render_status_html(status))
return JSONResponse(status.model_dump(mode="json"))