| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- import asyncio
- from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
- from sqlalchemy import text
- from sqlalchemy.orm import Session
- from core_domain import ServiceDescriptor, ServiceHealth
- from app.bootstrap.settings import ApiGatewaySettings
- from app.db.session import get_db
- from app.domain.repositories import ApiKeyRepository, GatewayRequestAuditRepository
- from app.infrastructure.api_keys import generate_api_key, get_api_key_prefix, hash_api_key
- from app.infrastructure.proxy import ProxyServiceName, ProxyTarget, ServiceProxy
- from app.schemas.gateway import (
- ApiKeyCreateRequest,
- ApiKeyCreateResponse,
- ApiKeyResponse,
- ApiKeyStatusUpdateRequest,
- GatewayRequestAuditResponse,
- GatewayServicesHealthResponse,
- )
- router = APIRouter()
- @router.get("/health", response_model=ServiceDescriptor)
- def health_check(db: Session = Depends(get_db)) -> ServiceDescriptor:
- db.execute(text("SELECT 1"))
- return ServiceDescriptor(name="api-gateway")
- @router.get("/ready", response_model=ServiceHealth)
- def readiness_check(db: Session = Depends(get_db)) -> ServiceHealth:
- db.execute(text("SELECT 1"))
- return ServiceHealth(service="api-gateway", status="ok", database="ok")
- @router.post("/gateway/api-keys", response_model=ApiKeyCreateResponse)
- def create_api_key(
- payload: ApiKeyCreateRequest,
- db: Session = Depends(get_db),
- ) -> ApiKeyCreateResponse:
- api_key = generate_api_key()
- entity = ApiKeyRepository(db).create(
- tenant_id=payload.tenant_id,
- name=payload.name,
- key_prefix=get_api_key_prefix(api_key),
- key_hash=hash_api_key(api_key),
- scopes=payload.scopes,
- expires_time=payload.expires_time,
- )
- return ApiKeyCreateResponse(
- id=entity.id,
- tenant_id=entity.tenant_id,
- name=entity.name,
- key_prefix=entity.key_prefix,
- api_key=api_key,
- status=entity.status,
- scopes=entity.scopes,
- expires_time=entity.expires_time,
- created_time=entity.created_time,
- )
- @router.get("/gateway/api-keys", response_model=list[ApiKeyResponse])
- def list_api_keys(
- tenant_id: str = Query(...),
- db: Session = Depends(get_db),
- ) -> list[ApiKeyResponse]:
- return [
- ApiKeyResponse.from_entity(item)
- for item in ApiKeyRepository(db).list_by_tenant(tenant_id=tenant_id)
- ]
- @router.patch("/gateway/api-keys/{api_key_id}/status", response_model=ApiKeyResponse)
- def update_api_key_status(
- api_key_id: str,
- payload: ApiKeyStatusUpdateRequest,
- db: Session = Depends(get_db),
- ) -> ApiKeyResponse:
- entity = ApiKeyRepository(db).update_status(
- tenant_id=payload.tenant_id,
- api_key_id=api_key_id,
- status=payload.status,
- )
- if entity is None:
- raise HTTPException(status_code=404, detail=f"api key not found: {api_key_id}")
- return ApiKeyResponse.from_entity(entity)
- @router.get("/gateway/audits", response_model=list[GatewayRequestAuditResponse])
- def list_gateway_audits(
- tenant_id: str = Query(...),
- request_id: str | None = Query(default=None),
- target_service: str | None = Query(default=None),
- limit: int = Query(default=100, ge=1, le=500),
- db: Session = Depends(get_db),
- ) -> list[GatewayRequestAuditResponse]:
- items = GatewayRequestAuditRepository(db).list_by_scope(
- tenant_id=tenant_id,
- request_id=request_id,
- target_service=target_service,
- limit=limit,
- )
- return [GatewayRequestAuditResponse.from_entity(item) for item in items]
- def get_gateway_settings() -> ApiGatewaySettings:
- return ApiGatewaySettings()
- def get_service_proxy(settings: ApiGatewaySettings = Depends(get_gateway_settings)) -> ServiceProxy:
- return ServiceProxy(timeout_seconds=settings.proxy_timeout_seconds)
- def build_proxy_targets(settings: ApiGatewaySettings) -> dict[ProxyServiceName, ProxyTarget]:
- return {
- "workflow-service": ProxyTarget(
- service_name="workflow-service",
- base_url=settings.workflow_service_url,
- path_prefix="/workflows",
- health_path="/workflows/health",
- ),
- "session-service": ProxyTarget(
- service_name="session-service",
- base_url=settings.session_service_url,
- path_prefix="/sessions",
- health_path="/sessions/health",
- ),
- "runtime-service": ProxyTarget(
- service_name="runtime-service",
- base_url=settings.runtime_service_url,
- path_prefix="/runtime",
- health_path="/runtime/health",
- ),
- "tool-service": ProxyTarget(
- service_name="tool-service",
- base_url=settings.tool_service_url,
- path_prefix="/tools",
- health_path="/tools/health",
- ),
- "model-gateway-service": ProxyTarget(
- service_name="model-gateway-service",
- base_url=settings.model_gateway_service_url,
- path_prefix="/models",
- health_path="/models/health",
- ),
- "code-runner-service": ProxyTarget(
- service_name="code-runner-service",
- base_url=settings.code_runner_service_url,
- path_prefix="/code",
- health_path="/code/health",
- ),
- "agent-service": ProxyTarget(
- service_name="agent-service",
- base_url=settings.agent_service_url,
- path_prefix="/agents",
- health_path="/agents/health",
- ),
- }
- @router.get("/gateway/services/health", response_model=GatewayServicesHealthResponse)
- async def downstream_health_check(
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- ) -> GatewayServicesHealthResponse:
- targets = build_proxy_targets(settings)
- health_proxy = ServiceProxy(timeout_seconds=settings.downstream_health_timeout_seconds)
- downstream_services = await asyncio.gather(
- *[health_proxy.check_health(target) for target in targets.values()]
- )
- status = "ok" if all(item.status == "ok" for item in downstream_services) else "degraded"
- return GatewayServicesHealthResponse(
- status=status,
- downstream_services=downstream_services,
- )
- @router.api_route(
- "/gateway/workflows",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/workflows/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_workflow_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["workflow-service"],
- path=path,
- )
- @router.api_route(
- "/gateway/sessions",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/sessions/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_session_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["session-service"],
- path=path,
- )
- @router.api_route(
- "/gateway/runtime",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/runtime/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_runtime_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["runtime-service"],
- path=path,
- )
- @router.api_route(
- "/gateway/agents",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/agents/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_agent_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["agent-service"],
- path=path,
- )
- @router.api_route(
- "/gateway/tools",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/tools/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_tool_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["tool-service"],
- path=path,
- )
- @router.api_route(
- "/gateway/models",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/models/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_model_gateway_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["model-gateway-service"],
- path=path,
- )
- @router.api_route(
- "/gateway/code",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- @router.api_route(
- "/gateway/code/{path:path}",
- methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
- )
- async def proxy_code_runner_service(
- request: Request,
- path: str = "",
- settings: ApiGatewaySettings = Depends(get_gateway_settings),
- proxy: ServiceProxy = Depends(get_service_proxy),
- ) -> Response:
- return await proxy.forward(
- request=request,
- target=build_proxy_targets(settings)["code-runner-service"],
- path=path,
- )
|