2026-05-30 21:53:09 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
from yuxi.channel.domain.port.channel_adapter_port import ChannelAdapterPort
|
2026-05-31 17:38:33 +08:00
|
|
|
from yuxi.channel.domain.port.channel_route_contributor_port import ChannelRouteContributorPort
|
2026-05-30 21:53:09 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register_all_channel_routes(
|
|
|
|
|
app: FastAPI,
|
|
|
|
|
adapters: dict[str, ChannelAdapterPort],
|
|
|
|
|
) -> list[str]:
|
|
|
|
|
registered: list[str] = []
|
|
|
|
|
|
|
|
|
|
for name, adapter in adapters.items():
|
|
|
|
|
contributor = _get_route_contributor(adapter)
|
|
|
|
|
if contributor is None:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
router = contributor.router
|
|
|
|
|
prefix = f"/channel/{name}"
|
|
|
|
|
app.include_router(router, prefix=prefix)
|
|
|
|
|
registered.append(prefix)
|
|
|
|
|
logger.info("registered channel routes: %s", prefix)
|
|
|
|
|
|
|
|
|
|
return registered
|
|
|
|
|
|
|
|
|
|
|
2026-05-31 17:38:33 +08:00
|
|
|
def _get_route_contributor(adapter: ChannelAdapterPort) -> ChannelRouteContributorPort | None:
|
2026-05-30 21:53:09 +08:00
|
|
|
contributor = getattr(adapter, "route_contributor", None)
|
|
|
|
|
if contributor is None:
|
|
|
|
|
return None
|
2026-05-31 17:38:33 +08:00
|
|
|
if isinstance(contributor, ChannelRouteContributorPort):
|
2026-05-30 21:53:09 +08:00
|
|
|
return contributor
|
|
|
|
|
if hasattr(contributor, "router"):
|
|
|
|
|
return contributor
|
|
|
|
|
return None
|