Files
NebulaShell/store/@{FutureOSS}/ws-api/middleware.py
Falck 76147bae94 初始提交 - FutureOSS v1.0 插件化运行时框架
一切皆为插件的开发者工具运行时框架

🧩 核心特性:
  - 插件热插拔 (importlib 动态加载)
  - 依赖自动解析 (拓扑排序 + 循环检测)
  - 企业级稳定 (熔断/降级/重试/隔离)
  - 事件驱动 (发布/订阅事件总线)
  - 完整配置 (YAML 配置 + 热重载)
2026-04-06 09:57:10 +08:00

42 lines
1.1 KiB
Python

"""WebSocket 中间件链"""
from typing import Callable, Optional, Any
class WsMiddleware:
"""WebSocket 中间件基类"""
async def process(self, client: Any, message: str, next_fn: Callable) -> Optional[str]:
"""处理消息"""
return await next_fn()
class AuthMiddleware(WsMiddleware):
"""认证中间件"""
async def process(self, client, message, next_fn):
# 可以在这里验证 token
return await next_fn()
class WsMiddlewareChain:
"""WebSocket 中间件链"""
def __init__(self):
self.middlewares: list[WsMiddleware] = []
def add(self, middleware: WsMiddleware):
"""添加中间件"""
self.middlewares.append(middleware)
async def run(self, client, message) -> Optional[str]:
"""执行中间件链"""
idx = 0
async def next_fn():
nonlocal idx
if idx < len(self.middlewares):
mw = self.middlewares[idx]
idx += 1
return await mw.process(client, message, next_fn)
return message
return await next_fn()