Files
RadioPropagationApi/api/app/main.py
T
2026-06-23 13:57:33 +03:00

50 lines
1.6 KiB
Python

from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from app.config import get_settings
from app.routers import api_router
def create_app() -> FastAPI:
settings = get_settings()
app = FastAPI(
title=settings.app_name,
version="0.1.0",
description="HTTP API for RF visibility, terrain profiles, coverage, and link budget.",
)
@app.get("/health", tags=["health"])
def health() -> dict[str, str]:
return {"status": "ok"}
@app.exception_handler(StarletteHTTPException)
def http_exception_handler(_request: Request, exc: StarletteHTTPException) -> JSONResponse:
detail = exc.detail
if not (isinstance(detail, dict) and "code" in detail and "detail" in detail):
detail = {"code": "HTTP_ERROR", "detail": detail}
return JSONResponse(status_code=exc.status_code, content={"detail": detail})
@app.exception_handler(RequestValidationError)
def validation_exception_handler(
_request: Request,
exc: RequestValidationError,
) -> JSONResponse:
return JSONResponse(
status_code=422,
content={
"detail": {
"code": "VALIDATION_ERROR",
"detail": "Request validation failed",
"errors": exc.errors(),
}
},
)
app.include_router(api_router, prefix=settings.api_v1_prefix)
return app
app = create_app()