feat: 添加 --webui-dir 启动参数以支持指定 WebUI 构建文件目录 (#2680)
* 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>
This commit is contained in:
@@ -22,6 +22,7 @@ class InitialLoader:
|
|||||||
self.db = db
|
self.db = db
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.log_broker = log_broker
|
self.log_broker = log_broker
|
||||||
|
self.webui_dir: str | None = None
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
core_lifecycle = AstrBotCoreLifecycle(self.log_broker, self.db)
|
core_lifecycle = AstrBotCoreLifecycle(self.log_broker, self.db)
|
||||||
@@ -35,8 +36,10 @@ class InitialLoader:
|
|||||||
|
|
||||||
core_task = core_lifecycle.start()
|
core_task = core_lifecycle.start()
|
||||||
|
|
||||||
|
webui_dir = self.webui_dir
|
||||||
|
|
||||||
self.dashboard_server = AstrBotDashboard(
|
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(
|
task = asyncio.gather(
|
||||||
core_task, self.dashboard_server.run()
|
core_task, self.dashboard_server.run()
|
||||||
|
|||||||
@@ -29,10 +29,19 @@ class AstrBotDashboard:
|
|||||||
core_lifecycle: AstrBotCoreLifecycle,
|
core_lifecycle: AstrBotCoreLifecycle,
|
||||||
db: BaseDatabase,
|
db: BaseDatabase,
|
||||||
shutdown_event: asyncio.Event,
|
shutdown_event: asyncio.Event,
|
||||||
|
webui_dir: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.core_lifecycle = core_lifecycle
|
self.core_lifecycle = core_lifecycle
|
||||||
self.config = core_lifecycle.astrbot_config
|
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="/")
|
self.app = Quart("dashboard", static_folder=self.data_path, static_url_path="/")
|
||||||
APP = self.app # noqa
|
APP = self.app # noqa
|
||||||
self.app.config["MAX_CONTENT_LENGTH"] = (
|
self.app.config["MAX_CONTENT_LENGTH"] = (
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ import os
|
|||||||
import asyncio
|
import asyncio
|
||||||
import sys
|
import sys
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import argparse
|
||||||
from astrbot.core.initial_loader import InitialLoader
|
from astrbot.core.initial_loader import InitialLoader
|
||||||
from astrbot.core import db_helper
|
from astrbot.core import db_helper
|
||||||
from astrbot.core import logger, LogManager, LogBroker
|
from astrbot.core import logger, LogManager, LogBroker
|
||||||
from astrbot.core.config.default import VERSION
|
from astrbot.core.config.default import VERSION
|
||||||
from astrbot.core.utils.io import download_dashboard, get_dashboard_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
|
# add parent path to sys.path
|
||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
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")
|
mimetypes.add_type("application/json", ".json")
|
||||||
|
|
||||||
|
|
||||||
async def check_dashboard_files():
|
async def check_dashboard_files(webui_dir: str | None = None):
|
||||||
"""下载管理面板文件"""
|
"""下载管理面板文件"""
|
||||||
|
# 指定webui目录
|
||||||
v = await get_dashboard_version()
|
if webui_dir:
|
||||||
if v is not None:
|
if os.path.exists(webui_dir):
|
||||||
# has file
|
logger.info(f"使用指定的 WebUI 目录: {webui_dir}")
|
||||||
if v == f"v{VERSION}":
|
return webui_dir
|
||||||
logger.info("WebUI 版本已是最新。")
|
|
||||||
else:
|
else:
|
||||||
logger.warning(
|
logger.warning(f"指定的 WebUI 目录 {webui_dir} 不存在,将使用默认逻辑。")
|
||||||
f"检测到 WebUI 版本 ({v}) 与当前 AstrBot 版本 (v{VERSION}) 不符。"
|
|
||||||
)
|
data_dist_path = os.path.join(get_astrbot_data_path(), "dist")
|
||||||
return
|
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(
|
logger.info(
|
||||||
"开始下载管理面板文件...高峰期(晚上)可能导致较慢的速度。如多次下载失败,请前往 https://github.com/Soulter/AstrBot/releases/latest 下载 dist.zip,并将其中的 dist 文件夹解压至 data 目录下。"
|
"开始下载管理面板文件...高峰期(晚上)可能导致较慢的速度。如多次下载失败,请前往 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)
|
await download_dashboard(version=f"v{VERSION}", latest=False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.critical(f"下载管理面板文件失败: {e}。")
|
logger.critical(f"下载管理面板文件失败: {e}。")
|
||||||
return
|
return None
|
||||||
|
|
||||||
logger.info("管理面板下载完成。")
|
logger.info("管理面板下载完成。")
|
||||||
|
return data_dist_path
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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()
|
check_env()
|
||||||
|
|
||||||
# start log broker
|
# start log broker
|
||||||
@@ -72,7 +90,7 @@ if __name__ == "__main__":
|
|||||||
LogManager.set_queue_handler(logger, log_broker)
|
LogManager.set_queue_handler(logger, log_broker)
|
||||||
|
|
||||||
# check dashboard files
|
# check dashboard files
|
||||||
asyncio.run(check_dashboard_files())
|
webui_dir = asyncio.run(check_dashboard_files(args.webui_dir))
|
||||||
|
|
||||||
db = db_helper
|
db = db_helper
|
||||||
|
|
||||||
@@ -80,4 +98,5 @@ if __name__ == "__main__":
|
|||||||
logger.info(logo_tmpl)
|
logger.info(logo_tmpl)
|
||||||
|
|
||||||
core_lifecycle = InitialLoader(db, log_broker)
|
core_lifecycle = InitialLoader(db, log_broker)
|
||||||
|
core_lifecycle.webui_dir = webui_dir
|
||||||
asyncio.run(core_lifecycle.start())
|
asyncio.run(core_lifecycle.start())
|
||||||
|
|||||||
Reference in New Issue
Block a user