From 68b8a1a01c94a366178e6dade1eaab478a733e7e Mon Sep 17 00:00:00 2001 From: LIghtJUNction Date: Fri, 6 Feb 2026 03:13:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86enable=E5=8F=98=E9=87=8F=E5=90=AB?= =?UTF-8?q?=E4=B9=89=E9=87=8A=E4=B8=BA=EF=BC=9A=E6=98=AF=E5=90=A6=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E9=9B=86=E6=88=90=E5=89=8D=E7=AB=AF=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E4=B8=BAFalse,=E4=BF=9D=E7=95=99=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E8=83=BD=E5=8A=9B=EF=BC=8C=E8=80=8C=E4=B8=8D=E6=98=AF=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E4=B9=9F=E5=85=B3=E9=97=AD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/dashboard/server.py | 69 +++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/astrbot/dashboard/server.py b/astrbot/dashboard/server.py index 5c2831386..e621dbae8 100644 --- a/astrbot/dashboard/server.py +++ b/astrbot/dashboard/server.py @@ -35,6 +35,7 @@ from .routes.t2i import T2iRoute APP: Quart + class AstrBotDashboard: """AstrBot Web Dashboard""" @@ -57,6 +58,8 @@ class AstrBotDashboard: self.config = core_lifecycle.astrbot_config self.shutdown_event = shutdown_event + self.enable_webui = self._check_webui_enabled() + self._init_paths(webui_dir) self._init_app() self.context = RouteContext(self.config, self.app) @@ -69,6 +72,13 @@ class AstrBotDashboard: # 初始化阶段 # ------------------------------------------------------------------ + def _check_webui_enabled(self) -> bool: + cfg = self.config.get("dashboard", {}) + _env = os.environ.get("DASHBOARD_ENABLE") + if _env is not None: + return _env.lower() in ("true", "1", "yes") + return cfg.get("enable", True) + def _init_paths(self, webui_dir: str | None): if webui_dir and os.path.exists(webui_dir): self.data_path = os.path.abspath(webui_dir) @@ -78,12 +88,28 @@ class AstrBotDashboard: ) def _init_app(self): - self.app = Quart( - "dashboard", - static_folder=self.data_path, - static_url_path="/", - ) - APP = self.app + if self.enable_webui: + self.app = Quart( + "dashboard", + static_folder=self.data_path, + static_url_path="/", + ) + else: + # 禁用 WebUI 时不提供静态文件服务 + self.app = Quart("dashboard") + + # 添加根路径提示 + @self.app.route("/") + async def index(): + return jsonify( + { + "message": "AstrBot WebUI has been detached.", + "status": "API Server Running", + "version": VERSION, + } + ) + + APP = self.app self.app = cors( self.app, allow_origin="*", allow_methods="*", allow_headers="*" ) @@ -245,29 +271,28 @@ class AstrBotDashboard: port: int = int(_port) _host = os.environ.get("DASHBOARD_HOST") or cfg.get("host", "::") host: str = _host.strip("[]") - _env = os.environ.get("DASHBOARD_ENABLE") - enable = ( - _env.lower() in ("true", "1", "yes") - if _env is not None - else cfg.get("enable", True) - ) - - if not enable: - logger.info("WebUI 已被禁用") - return None display_host = f"[{host}]" if ":" in host else host - logger.info( - "正在启动 WebUI, 监听地址: http://%s:%s", - display_host, - port, - ) + + if self.enable_webui: + logger.info( + "正在启动 WebUI + API, 监听地址: http://%s:%s", + display_host, + port, + ) + else: + logger.info( + "正在启动 API Server (WebUI 已分离), 监听地址: http://%s:%s", + display_host, + port, + ) if self.check_port_in_use("127.0.0.1", port): info = self.get_process_using_port(port) raise RuntimeError(f"端口 {port} 已被占用\n{info}") - self._print_access_urls(host, port) + if self.enable_webui: + self._print_access_urls(host, port) config = HyperConfig() binds: list[str] = [self._build_bind(host, port)]