63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
import logging
|
|
|
|
from yuxi.channel.gateway.routes import webhook_registry
|
|
from yuxi.channel.gateway.webhook_security import WebhookGuardConfig
|
|
|
|
logger = logging.getLogger("yuxi.channel.generic_webhook.gateway")
|
|
|
|
|
|
class GenericWebhookGateway:
|
|
def __init__(self, config_mgr, deduplicator, mapping_engine):
|
|
self._config_mgr = config_mgr
|
|
self._deduplicator = deduplicator
|
|
self._mapping_engine = mapping_engine
|
|
self._running = False
|
|
|
|
async def start(self, ctx) -> object:
|
|
from yuxi.channel.extensions.generic_webhook.webhook import set_runtime_state
|
|
|
|
set_runtime_state(
|
|
config_mgr=self._config_mgr,
|
|
deduplicator=self._deduplicator,
|
|
mapping_engine=self._mapping_engine,
|
|
)
|
|
|
|
endpoints = self._config_mgr.list_endpoint_ids()
|
|
self._register_webhook_handler()
|
|
|
|
if not endpoints:
|
|
logger.info("Gateway 已启动(无预注册端点,等待动态添加)")
|
|
else:
|
|
logger.info(
|
|
"Gateway 已启动,已注册 %d 个端点: %s",
|
|
len(endpoints),
|
|
", ".join(endpoints),
|
|
)
|
|
|
|
self._running = True
|
|
return self
|
|
|
|
def _register_webhook_handler(self):
|
|
from yuxi.channel.extensions.generic_webhook.webhook import generic_webhook_handler
|
|
|
|
guard_config = WebhookGuardConfig(
|
|
channel_type="generic-webhook",
|
|
allowed_methods=("POST",),
|
|
allowed_content_types=("application/json",),
|
|
body_size_limit=1_048_576,
|
|
)
|
|
webhook_registry.register(
|
|
"generic-webhook",
|
|
generic_webhook_handler,
|
|
guard_config=guard_config,
|
|
)
|
|
|
|
async def stop(self, ctx=None):
|
|
webhook_registry.remove("generic-webhook")
|
|
self._running = False
|
|
logger.info("Gateway 已停止")
|
|
|
|
@property
|
|
def is_running(self) -> bool:
|
|
return self._running
|