from __future__ import annotations import logging from fastapi import FastAPI from yuxi.channel.domain.port.channel_adapter_port import ChannelAdapterPort from yuxi.channel.domain.port.channel_route_contributor_port import ChannelRouteContributorPort 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 def _get_route_contributor(adapter: ChannelAdapterPort) -> ChannelRouteContributorPort | None: contributor = getattr(adapter, "route_contributor", None) if contributor is None: return None if isinstance(contributor, ChannelRouteContributorPort): return contributor if hasattr(contributor, "router"): return contributor return None