41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
"""插件 Web 子应用样例。
|
||
|
||
- 由 `__init__.py` 里 `register_web_plugin("template", ..., module_name=__name__)` 注册,
|
||
hub 启动时自动挂载到 `/api/template`。
|
||
- 鉴权统一走 `hexi.web_hub.web_auth.require_admin`(OAuth2 + SQLite);注意它本身是 FastAPI 的
|
||
`Depends` 实例,直接用 `_: dict = API` 注入,不要再包一层 `Depends(...)`。
|
||
- 可直接读写 `hexi.web_hub.web_config` 的插件配置。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from fastapi import FastAPI
|
||
from fastapi.responses import HTMLResponse
|
||
|
||
from hexi import web_config
|
||
from hexi.web_hub.web_auth import require_admin as API
|
||
|
||
# 插件模块名(去掉 .web):= NoneBot 模块名 = plugin_id
|
||
_PLUGIN_ID = __package__.rsplit(".", 1)[0]
|
||
|
||
|
||
def build_admin_app() -> FastAPI:
|
||
app = FastAPI(title="插件模板管理")
|
||
|
||
@app.get("/")
|
||
async def index(_: dict = API) -> HTMLResponse:
|
||
return HTMLResponse("<h1>插件模板 · Web 子应用</h1><p>鉴权已生效。</p>")
|
||
|
||
@app.get("/ping")
|
||
async def ping(_: dict = API):
|
||
return {"ok": True, "plugin": _PLUGIN_ID}
|
||
|
||
@app.get("/config")
|
||
async def get_config(_: dict = API):
|
||
cfg = web_config.get_config(_PLUGIN_ID)
|
||
if cfg is None:
|
||
return {"ok": False, "msg": "未注册配置 schema"}
|
||
return {"ok": True, "plugin": _PLUGIN_ID, **cfg}
|
||
|
||
return app
|