ira/sim-search-api/app/main.py

81 lines
2.7 KiB
Python

"""
Main FastAPI application for the sim-search API.
This module defines the FastAPI application and includes all routes.
"""
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.utils import get_openapi
from app.api.routes import query, search, report, auth
from app.core.config import settings
# Create FastAPI app
app = FastAPI(
title=settings.PROJECT_NAME,
description=settings.PROJECT_DESCRIPTION,
version=settings.VERSION,
docs_url=None, # Disable default docs
redoc_url=None, # Disable default redoc
)
# Set up CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(auth.router, prefix=f"{settings.API_V1_STR}/auth", tags=["Authentication"])
app.include_router(query.router, prefix=f"{settings.API_V1_STR}/query", tags=["Query Processing"])
app.include_router(search.router, prefix=f"{settings.API_V1_STR}/search", tags=["Search Execution"])
app.include_router(report.router, prefix=f"{settings.API_V1_STR}/report", tags=["Report Generation"])
# Custom OpenAPI and documentation endpoints
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
"""Custom Swagger UI documentation."""
return get_swagger_ui_html(
openapi_url=f"{settings.API_V1_STR}/openapi.json",
title=f"{settings.PROJECT_NAME} - Swagger UI",
oauth2_redirect_url=f"{settings.API_V1_STR}/docs/oauth2-redirect",
swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css",
)
@app.get(f"{settings.API_V1_STR}/openapi.json", include_in_schema=False)
async def get_open_api_endpoint():
"""Return OpenAPI schema."""
return get_openapi(
title=settings.PROJECT_NAME,
version=settings.VERSION,
description=settings.PROJECT_DESCRIPTION,
routes=app.routes,
)
@app.get("/", tags=["Status"])
async def root():
"""Root endpoint to check API status."""
return {
"status": "online",
"version": settings.VERSION,
"project": settings.PROJECT_NAME,
"docs": "/docs"
}
# Initialize components on startup
@app.on_event("startup")
async def startup_event():
"""Initialize components on startup."""
# Import here to avoid circular imports
from app.services.report_service import initialize_report_generator
# Initialize report generator
await initialize_report_generator()