mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
perf: 动态路由注册
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user