|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import inspect |
| 4 | +import os |
| 5 | + |
| 6 | +import rtoml |
| 7 | + |
| 8 | +from fastapi import FastAPI |
| 9 | + |
| 10 | +from backend.core.path_conf import PLUGIN_DIR |
| 11 | +from backend.utils.import_parse import import_module_cached |
| 12 | + |
| 13 | + |
| 14 | +def get_plugins() -> list[str]: |
| 15 | + """获取插件""" |
| 16 | + plugin_packages = [] |
| 17 | + |
| 18 | + for item in os.listdir(PLUGIN_DIR): |
| 19 | + item_path = os.path.join(PLUGIN_DIR, item) |
| 20 | + |
| 21 | + if os.path.isdir(item_path): |
| 22 | + if '__init__.py' in os.listdir(item_path): |
| 23 | + plugin_packages.append(item) |
| 24 | + |
| 25 | + return plugin_packages |
| 26 | + |
| 27 | + |
| 28 | +def get_plugin_models() -> list: |
| 29 | + """获取插件所有模型类""" |
| 30 | + classes = [] |
| 31 | + plugins = get_plugins() |
| 32 | + for plugin in plugins: |
| 33 | + module_path = f'backend.plugin.{plugin}.model' |
| 34 | + module = import_module_cached(module_path) |
| 35 | + for name, obj in inspect.getmembers(module): |
| 36 | + if inspect.isclass(obj): |
| 37 | + classes.append(obj) |
| 38 | + return classes |
| 39 | + |
| 40 | + |
| 41 | +def register_plugin_router(app: FastAPI): |
| 42 | + """ |
| 43 | + 插件路由注入 |
| 44 | +
|
| 45 | + :param app: |
| 46 | + :return: |
| 47 | + """ |
| 48 | + plugins = get_plugins() |
| 49 | + for plugin in plugins: |
| 50 | + toml_path = os.path.join(PLUGIN_DIR, plugin, 'plugin.toml') |
| 51 | + if not os.path.exists(toml_path): |
| 52 | + raise FileNotFoundError('插件缺少 plugin.toml 配置文件') |
| 53 | + |
| 54 | + toml_data = rtoml.load(toml_path) |
| 55 | + app_name = toml_data.get('app', '') |
| 56 | + api_module = toml_data.get('api', {}).get('module', '') |
| 57 | + prefix = toml_data.get('api', {}).get('prefix', '') |
| 58 | + |
| 59 | + if app_name: |
| 60 | + if '.' in api_module: |
| 61 | + module_path = f'backend.app.{app_name}.api.{api_module}' |
| 62 | + else: |
| 63 | + module_path = f'backend.app.{app_name}.api' |
| 64 | + else: |
| 65 | + module_path = 'backend.app' |
| 66 | + |
| 67 | + try: |
| 68 | + module = import_module_cached(module_path) |
| 69 | + except ImportError as e: |
| 70 | + raise ImportError(f'导入模块 {module_path} 失败:{e}') from e |
| 71 | + else: |
| 72 | + if app_name and '.' in api_module: |
| 73 | + # 从 backend.app.xxx.api.vx.xxx.__init__.py 文件中获取路由 |
| 74 | + router = getattr(module, 'router', None) |
| 75 | + else: |
| 76 | + if 'api' in module_path: |
| 77 | + # 从 backend.app.xxx.api 下的 router.py 文件中获取路由 |
| 78 | + router = getattr(module, api_module.split('.')[0], None) |
| 79 | + else: |
| 80 | + # 从 backend.app 下的 router.py 文件中获取路由 |
| 81 | + router = getattr(module, 'router', None) |
| 82 | + |
| 83 | + if not router: |
| 84 | + raise ImportError(f'模块 {module_path} 中不存在路由') |
| 85 | + |
| 86 | + if app_name: |
| 87 | + app.include_router(router, prefix=f'/{prefix}') |
| 88 | + else: |
| 89 | + app.include_router(router) |
0 commit comments