79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
"""
|
|
Main entry point for the Loan Operations API.
|
|
"""
|
|
import sentry_sdk
|
|
from ddtrace import patch_all
|
|
from fastapi import APIRouter, FastAPI
|
|
from sentry_sdk.integrations.fastapi import FastApiIntegration
|
|
from slowapi import _rate_limit_exceeded_handler
|
|
from slowapi.errors import RateLimitExceeded
|
|
from slowapi.middleware import SlowAPIMiddleware
|
|
|
|
from app.config import settings
|
|
from app.logging import configure_logging, get_logger
|
|
from app.middleware import logging_middleware, limiter
|
|
from app.resources import health
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""Create and configure the FastAPI application."""
|
|
|
|
# Configure monitoring
|
|
if settings.sentry_dsn:
|
|
sentry_sdk.init(
|
|
dsn=settings.sentry_dsn,
|
|
integrations=[FastApiIntegration()],
|
|
traces_sample_rate=1.0,
|
|
environment=settings.dd_env,
|
|
)
|
|
|
|
if settings.dd_service:
|
|
# Configure Datadog tracing
|
|
patch_all()
|
|
|
|
# Configure logging
|
|
configure_logging()
|
|
logger = get_logger(__name__)
|
|
|
|
# Create FastAPI app
|
|
app = FastAPI(
|
|
title="Loan Operations API",
|
|
description="SBA Loan Operations API",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
)
|
|
|
|
# Add rate limiting middleware if enabled
|
|
if settings.rate_limit_enabled:
|
|
app.state.limiter = limiter
|
|
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
|
app.add_middleware(SlowAPIMiddleware)
|
|
|
|
# Add logging middleware
|
|
app.middleware("http")(logging_middleware)
|
|
|
|
# Include all endpoint routers
|
|
app.include_router(health.router, tags=["health"])
|
|
|
|
return app
|
|
|
|
|
|
def main() -> None:
|
|
"""Run the application."""
|
|
|
|
import uvicorn
|
|
|
|
uvicorn.run(
|
|
app,
|
|
host=settings.host,
|
|
port=settings.port,
|
|
log_level=settings.log_level.lower(),
|
|
reload=settings.debug,
|
|
)
|
|
|
|
app = create_app()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|