perf: 动态路由注册

This commit is contained in:
Soulter
2025-05-23 15:18:16 +08:00
parent 0b0e4ce904
commit ad51381063
2 changed files with 18 additions and 6 deletions

View File

@@ -102,7 +102,10 @@ class PluginRoute(Route):
async def get_plugins(self):
_plugin_resp = []
plugin_name = request.args.get("name")
for plugin in self.plugin_manager.context.get_all_stars():
if plugin_name and plugin.name != plugin_name:
continue
_t = {
"name": plugin.name,
"repo": "" if plugin.repo is None else plugin.repo,

View File

@@ -54,15 +54,24 @@ class AstrBotDashboard:
self.conversation_route = ConversationRoute(self.context, db, core_lifecycle)
self.file_route = FileRoute(self.context)
if self.core_lifecycle.star_context.registered_web_apis:
for api in self.core_lifecycle.star_context.registered_web_apis:
route, view_handler, methods, _ = api
self.app.add_url_rule(
f"/api/plug{route}", view_func=view_handler, methods=methods
)
self.app.add_url_rule(
"/api/plug/<path:subpath>",
view_func=self.srv_plug_route,
methods=["GET", "POST"],
)
self.shutdown_event = shutdown_event
async def srv_plug_route(self, subpath, *args, **kwargs):
"""
插件路由
"""
registered_web_apis = self.core_lifecycle.star_context.registered_web_apis
for api in registered_web_apis:
route, view_handler, methods, _ = api
if route == f"/{subpath}" and request.method in methods:
return await view_handler(*args, **kwargs)
async def auth_middleware(self):
if not request.path.startswith("/api"):
return