| """Thin HTTP handler — reads the request, delegates to the Router.""" |
| from __future__ import annotations |
|
|
| from typing import Optional |
|
|
| from fastapi import APIRouter, Request |
| from fastapi.responses import JSONResponse |
|
|
| from ..services.router import Router |
|
|
| router = APIRouter() |
| ROUTER: Optional[Router] = None |
|
|
|
|
| def init_router(config, client) -> None: |
| global ROUTER |
| ROUTER = Router(config, client) |
|
|
|
|
| @router.get("/v1/models") |
| async def list_models(): |
| return JSONResponse({"object": "list", "data": ROUTER.config.list_models()}) |
|
|
|
|
| @router.api_route( |
| "/v1/{path:path}", |
| methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"], |
| ) |
| async def proxy(path: str, request: Request): |
| |
| full_path = "v1/" + path |
| body_bytes = await request.body() |
| client_headers = dict(request.headers) |
| query_params = dict(request.query_params) |
| |
| user_id = getattr(request.state, "user_id", None) |
| is_admin = getattr(request.state, "is_admin", False) |
| return await ROUTER.handle(request.method, full_path, body_bytes, client_headers, |
| query_params, user_id=user_id, is_admin=is_admin) |
|
|