initial commit after split
This commit is contained in:
48
src/govoplan_core/server/fastapi.py
Normal file
48
src/govoplan_core/server/fastapi.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import AsyncIterator, Callable, Iterable
|
||||
from contextlib import AbstractAsyncContextManager
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from govoplan_core.core.registry import PlatformRegistry
|
||||
|
||||
LifespanFactory = Callable[[FastAPI], AbstractAsyncContextManager[None] | AsyncIterator[None]]
|
||||
|
||||
|
||||
def create_govoplan_app(
|
||||
*,
|
||||
title: str,
|
||||
version: str,
|
||||
registry: PlatformRegistry,
|
||||
api_router: APIRouter | None = None,
|
||||
lifespan: LifespanFactory | None = None,
|
||||
cors_origins: Iterable[str] = (),
|
||||
health_payload: dict[str, Any] | None = None,
|
||||
) -> FastAPI:
|
||||
app = FastAPI(title=title, version=version, lifespan=lifespan)
|
||||
app.state.govoplan_registry = registry
|
||||
|
||||
origins = [item.strip() for item in cors_origins if item.strip()]
|
||||
if origins:
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"] if "*" in origins else origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
if api_router is not None:
|
||||
app.include_router(api_router)
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
payload = {"status": "ok"}
|
||||
if health_payload:
|
||||
payload.update(health_payload)
|
||||
return payload
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user