mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-05 19:36:43 +08:00
🐛 not load webui when not install fastapi (#221)
This commit is contained in:
parent
2149e03be1
commit
39a3b64587
@ -2,12 +2,9 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import socketio
|
|
||||||
from fastapi.applications import FastAPI
|
|
||||||
from fastapi.staticfiles import StaticFiles
|
|
||||||
from nonebot import get_driver, on_command
|
from nonebot import get_driver, on_command
|
||||||
from nonebot.adapters.onebot.v11 import Bot
|
from nonebot.adapters.onebot.v11 import Bot
|
||||||
from nonebot.adapters.onebot.v11.event import GroupMessageEvent, PrivateMessageEvent
|
from nonebot.adapters.onebot.v11.event import PrivateMessageEvent
|
||||||
from nonebot.drivers.fastapi import Driver
|
from nonebot.drivers.fastapi import Driver
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from nonebot.rule import to_me
|
from nonebot.rule import to_me
|
||||||
@ -19,38 +16,42 @@ from .token_manager import token_manager as tm
|
|||||||
|
|
||||||
STATIC_PATH = (Path(__file__).parent / "dist").resolve()
|
STATIC_PATH = (Path(__file__).parent / "dist").resolve()
|
||||||
|
|
||||||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
|
|
||||||
socket_app = socketio.ASGIApp(sio, socketio_path="socket")
|
|
||||||
|
|
||||||
|
def init_fastapi():
|
||||||
|
import socketio
|
||||||
|
from fastapi.applications import FastAPI
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
class SinglePageApplication(StaticFiles):
|
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
|
||||||
def __init__(self, directory: os.PathLike, index="index.html"):
|
socket_app = socketio.ASGIApp(sio, socketio_path="socket")
|
||||||
self.index = index
|
|
||||||
super().__init__(directory=directory, packages=None, html=True, check_dir=True)
|
|
||||||
|
|
||||||
def lookup_path(self, path: str) -> tuple[str, Union[os.stat_result, None]]:
|
class SinglePageApplication(StaticFiles):
|
||||||
full_path, stat_res = super().lookup_path(path)
|
def __init__(self, directory: os.PathLike, index="index.html"):
|
||||||
if stat_res is None:
|
self.index = index
|
||||||
return super().lookup_path(self.index)
|
super().__init__(
|
||||||
return (full_path, stat_res)
|
directory=directory, packages=None, html=True, check_dir=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def lookup_path(self, path: str) -> tuple[str, Union[os.stat_result, None]]:
|
||||||
|
full_path, stat_res = super().lookup_path(path)
|
||||||
|
if stat_res is None:
|
||||||
|
return super().lookup_path(self.index)
|
||||||
|
return (full_path, stat_res)
|
||||||
|
|
||||||
def register_router_fastapi(driver: Driver, socketio):
|
def register_router_fastapi(driver: Driver, socketio):
|
||||||
static_path = STATIC_PATH
|
static_path = STATIC_PATH
|
||||||
nonebot_app = FastAPI(
|
nonebot_app = FastAPI(
|
||||||
title="nonebot-bison",
|
title="nonebot-bison",
|
||||||
description="nonebot-bison webui and api",
|
description="nonebot-bison webui and api",
|
||||||
)
|
)
|
||||||
nonebot_app.include_router(api_router)
|
nonebot_app.include_router(api_router)
|
||||||
nonebot_app.mount(
|
nonebot_app.mount(
|
||||||
"/", SinglePageApplication(directory=static_path), name="bison-frontend"
|
"/", SinglePageApplication(directory=static_path), name="bison-frontend"
|
||||||
)
|
)
|
||||||
|
|
||||||
app = driver.server_app
|
app = driver.server_app
|
||||||
app.mount("/bison", nonebot_app, "nonebot-bison")
|
app.mount("/bison", nonebot_app, "nonebot-bison")
|
||||||
|
|
||||||
|
|
||||||
def init():
|
|
||||||
driver = get_driver()
|
driver = get_driver()
|
||||||
if isinstance(driver, Driver):
|
if isinstance(driver, Driver):
|
||||||
register_router_fastapi(driver, socket_app)
|
register_router_fastapi(driver, socket_app)
|
||||||
@ -67,9 +68,7 @@ def init():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if (STATIC_PATH / "index.html").exists():
|
def register_get_token_handler():
|
||||||
init()
|
|
||||||
|
|
||||||
get_token = on_command("后台管理", rule=to_me(), priority=5, aliases={"管理后台"})
|
get_token = on_command("后台管理", rule=to_me(), priority=5, aliases={"管理后台"})
|
||||||
|
|
||||||
@get_token.handle()
|
@get_token.handle()
|
||||||
@ -77,8 +76,21 @@ if (STATIC_PATH / "index.html").exists():
|
|||||||
token = tm.get_user_token((event.get_user_id(), event.sender.nickname))
|
token = tm.get_user_token((event.get_user_id(), event.sender.nickname))
|
||||||
await get_token.finish(f"请访问: {plugin_config.bison_outer_url}auth/{token}")
|
await get_token.finish(f"请访问: {plugin_config.bison_outer_url}auth/{token}")
|
||||||
|
|
||||||
get_token.__help__name__ = "获取后台管理地址"
|
get_token.__help__name__ = "获取后台管理地址" # type: ignore
|
||||||
get_token.__help__info__ = "获取管理bot后台的地址,该地址会" "在一段时间过后过期,请不要泄漏该地址"
|
get_token.__help__info__ = "获取管理bot后台的地址,该地址会" "在一段时间过后过期,请不要泄漏该地址" # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
def check_driver_is_fastapi() -> bool:
|
||||||
|
driver = get_driver()
|
||||||
|
return driver.type == "fastapi"
|
||||||
|
|
||||||
|
|
||||||
|
if (STATIC_PATH / "index.html").exists():
|
||||||
|
if check_driver_is_fastapi():
|
||||||
|
init_fastapi()
|
||||||
|
register_get_token_handler()
|
||||||
|
else:
|
||||||
|
logger.warning("your driver is not fastapi, webui feature will be disabled")
|
||||||
else:
|
else:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Frontend file not found, please compile it or use docker or pypi version"
|
"Frontend file not found, please compile it or use docker or pypi version"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user