|
| 1 | +"""程序总入口""" |
| 2 | + |
| 3 | +from asyncio import CancelledError |
| 4 | +import tomllib |
| 5 | +from contextlib import asynccontextmanager |
| 6 | +import importlib |
| 7 | +import logging |
| 8 | +from pathlib import Path |
| 9 | +from fastapi import FastAPI |
| 10 | + |
| 11 | +log = logging.getLogger("uvicorn") |
| 12 | + |
| 13 | +loaded_plugins = {} |
| 14 | + |
| 15 | +# 使用绝对路径加载配置文件 |
| 16 | +config_path = Path(__file__).parent / "config.toml" |
| 17 | +try: |
| 18 | + with open(config_path, "rb") as f: |
| 19 | + _config_data = tomllib.load(f) |
| 20 | +except FileNotFoundError: |
| 21 | + log.error("配置文件未找到: %s", config_path) |
| 22 | + raise |
| 23 | + |
| 24 | +@asynccontextmanager |
| 25 | +async def lifespan(application: FastAPI): |
| 26 | + """ |
| 27 | + 应用生命周期管理 |
| 28 | + |
| 29 | + :param application: FastAPI应用实例 |
| 30 | + :type application: FastAPI |
| 31 | + """ |
| 32 | + try: |
| 33 | + log.info("CodeFeatrue-破晓之码 正在启动") |
| 34 | + # 存储插件事件映射 |
| 35 | + event_subscriptions = {} |
| 36 | + for plugin_name in _config_data["main"]["plugins"]: |
| 37 | + # 初始化插件 |
| 38 | + try: |
| 39 | + plugin = importlib.import_module("plugins." + plugin_name) |
| 40 | + loaded_plugins[plugin_name] = plugin |
| 41 | + plugin_meta = getattr(plugin, '__plugin_meta__', {}) |
| 42 | + events = plugin_meta.get('events', []) |
| 43 | + # 订阅事件 |
| 44 | + for event in events: |
| 45 | + if event not in event_subscriptions: |
| 46 | + event_subscriptions[event] = [] |
| 47 | + event_subscriptions[event].append(plugin_name) |
| 48 | + log.info("插件 %s 加载成功", plugin_name) |
| 49 | + # 调用插件启用函数 |
| 50 | + if hasattr(plugin, 'on_enable'): |
| 51 | + plugin.on_enable(application) |
| 52 | + except AttributeError as ae: |
| 53 | + log.warning("插件 %s 缺少必要的函数: %s", plugin_name, ae) |
| 54 | + # 将事件订阅信息存储到应用状态中 |
| 55 | + application.state.event_subscriptions = event_subscriptions |
| 56 | + log.info("事件订阅: %s", event_subscriptions) |
| 57 | + yield |
| 58 | + log.info("CodeFeatrue-破晓之码 正在退出") |
| 59 | + except (CancelledError, KeyboardInterrupt) as e: |
| 60 | + log.error("CodeFeatrue-破晓之码 启动失败: 在启动过程中被用户手动关闭。%s", e) |
| 61 | + raise |
| 62 | + |
| 63 | +app = FastAPI(lifespan=lifespan) |
| 64 | + |
| 65 | +@app.post("/") |
| 66 | +def main(info: dict): |
| 67 | + """ |
| 68 | + 处理接收到的消息 |
| 69 | + |
| 70 | + :param info: Onebot实现端传入的信息 |
| 71 | + :type info: dict |
| 72 | + """ |
| 73 | + post_type = info["post_type"] |
| 74 | + # 通知订阅了该事件的所有插件 |
| 75 | + event_subscriptions = app.state.event_subscriptions |
| 76 | + if post_type in event_subscriptions: |
| 77 | + for plugin_name in event_subscriptions[post_type]: |
| 78 | + plugin = loaded_plugins.get(plugin_name) |
| 79 | + if plugin and hasattr(plugin, 'on_event'): |
| 80 | + return_info = plugin.on_event(post_type, info) |
| 81 | + if return_info: |
| 82 | + return return_info |
| 83 | + else: |
| 84 | + log.info("插件 %s 未处理事件 %s", plugin_name, post_type) |
| 85 | + return { } |
0 commit comments