From d24902c66df660939814e4eea3b563e0c11347ac Mon Sep 17 00:00:00 2001 From: shangxue <1919892171@qq.com> Date: Fri, 12 Sep 2025 15:15:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20--webui-dir=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=8F=82=E6=95=B0=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=20WebUI=20=E6=9E=84=E5=BB=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9B=AE=E5=BD=95=20(#2680)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update main.py * Update server.py * Update main.py * Update main.py * Update main.py * Update initial_loader.py * Update server.py * Update main.py * chore: update webui_dir type hint and improve dashboard file check logic --------- Co-authored-by: Soulter <905617992@qq.com> --- astrbot/core/initial_loader.py | 5 +++- astrbot/dashboard/server.py | 11 ++++++++- main.py | 45 ++++++++++++++++++++++++---------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/astrbot/core/initial_loader.py b/astrbot/core/initial_loader.py index ac0ee277e..b3eeb6e88 100644 --- a/astrbot/core/initial_loader.py +++ b/astrbot/core/initial_loader.py @@ -22,6 +22,7 @@ class InitialLoader: self.db = db self.logger = logger self.log_broker = log_broker + self.webui_dir: str | None = None async def start(self): core_lifecycle = AstrBotCoreLifecycle(self.log_broker, self.db) @@ -35,8 +36,10 @@ class InitialLoader: core_task = core_lifecycle.start() + webui_dir = self.webui_dir + self.dashboard_server = AstrBotDashboard( - core_lifecycle, self.db, core_lifecycle.dashboard_shutdown_event + core_lifecycle, self.db, core_lifecycle.dashboard_shutdown_event, webui_dir ) task = asyncio.gather( core_task, self.dashboard_server.run() diff --git a/astrbot/dashboard/server.py b/astrbot/dashboard/server.py index 83f40c8f2..9b9588d43 100644 --- a/astrbot/dashboard/server.py +++ b/astrbot/dashboard/server.py @@ -29,10 +29,19 @@ class AstrBotDashboard: core_lifecycle: AstrBotCoreLifecycle, db: BaseDatabase, shutdown_event: asyncio.Event, + webui_dir: str | None = None, ) -> None: self.core_lifecycle = core_lifecycle self.config = core_lifecycle.astrbot_config - self.data_path = os.path.abspath(os.path.join(get_astrbot_data_path(), "dist")) + + # 参数指定webui目录 + if webui_dir and os.path.exists(webui_dir): + self.data_path = os.path.abspath(webui_dir) + else: + self.data_path = os.path.abspath( + os.path.join(get_astrbot_data_path(), "dist") + ) + self.app = Quart("dashboard", static_folder=self.data_path, static_url_path="/") APP = self.app # noqa self.app.config["MAX_CONTENT_LENGTH"] = ( diff --git a/main.py b/main.py index b453d4c34..b3c8e5893 100644 --- a/main.py +++ b/main.py @@ -2,11 +2,13 @@ import os import asyncio import sys import mimetypes +import argparse from astrbot.core.initial_loader import InitialLoader from astrbot.core import db_helper from astrbot.core import logger, LogManager, LogBroker from astrbot.core.config.default import VERSION from astrbot.core.utils.io import download_dashboard, get_dashboard_version +from astrbot.core.utils.astrbot_path import get_astrbot_data_path # add parent path to sys.path sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -37,19 +39,28 @@ def check_env(): mimetypes.add_type("application/json", ".json") -async def check_dashboard_files(): +async def check_dashboard_files(webui_dir: str | None = None): """下载管理面板文件""" - - v = await get_dashboard_version() - if v is not None: - # has file - if v == f"v{VERSION}": - logger.info("WebUI 版本已是最新。") + # 指定webui目录 + if webui_dir: + if os.path.exists(webui_dir): + logger.info(f"使用指定的 WebUI 目录: {webui_dir}") + return webui_dir else: - logger.warning( - f"检测到 WebUI 版本 ({v}) 与当前 AstrBot 版本 (v{VERSION}) 不符。" - ) - return + logger.warning(f"指定的 WebUI 目录 {webui_dir} 不存在,将使用默认逻辑。") + + data_dist_path = os.path.join(get_astrbot_data_path(), "dist") + if os.path.exists(data_dist_path): + v = await get_dashboard_version() + if v is not None: + # has file + if v == f"v{VERSION}": + logger.info("WebUI 版本已是最新。") + else: + logger.warning( + f"检测到 WebUI 版本 ({v}) 与当前 AstrBot 版本 (v{VERSION}) 不符。" + ) + return data_dist_path logger.info( "开始下载管理面板文件...高峰期(晚上)可能导致较慢的速度。如多次下载失败,请前往 https://github.com/Soulter/AstrBot/releases/latest 下载 dist.zip,并将其中的 dist 文件夹解压至 data 目录下。" @@ -59,12 +70,19 @@ async def check_dashboard_files(): await download_dashboard(version=f"v{VERSION}", latest=False) except Exception as e: logger.critical(f"下载管理面板文件失败: {e}。") - return + return None logger.info("管理面板下载完成。") + return data_dist_path if __name__ == "__main__": + parser = argparse.ArgumentParser(description="AstrBot") + parser.add_argument( + "--webui-dir", type=str, help="指定 WebUI 静态文件目录路径", default=None + ) + args = parser.parse_args() + check_env() # start log broker @@ -72,7 +90,7 @@ if __name__ == "__main__": LogManager.set_queue_handler(logger, log_broker) # check dashboard files - asyncio.run(check_dashboard_files()) + webui_dir = asyncio.run(check_dashboard_files(args.webui_dir)) db = db_helper @@ -80,4 +98,5 @@ if __name__ == "__main__": logger.info(logo_tmpl) core_lifecycle = InitialLoader(db, log_broker) + core_lifecycle.webui_dir = webui_dir asyncio.run(core_lifecycle.start())