You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.8 KiB
Python
110 lines
3.8 KiB
Python
import logging
|
|
import os
|
|
import sys
|
|
import time
|
|
from os import path
|
|
from uuid import uuid1
|
|
from typing import Optional, Final
|
|
|
|
from fastapi import FastAPI, HTTPException, status, Path
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import ValidationError
|
|
|
|
from slowapi import _rate_limit_exceeded_handler
|
|
from slowapi.errors import RateLimitExceeded
|
|
from sqlalchemy import inspect
|
|
from sqlalchemy.orm import scoped_session
|
|
from sqlalchemy.ext.asyncio import async_scoped_session
|
|
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
|
from starlette.requests import Request
|
|
from starlette.routing import compile_path
|
|
from starlette.middleware.gzip import GZipMiddleware
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from starlette.responses import Response, StreamingResponse, FileResponse
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
from src.enums import ResponseStatus
|
|
from src.logging import configure_logging
|
|
from src.rate_limiter import limiter
|
|
from src.api import api_router
|
|
from src.database.core import engine, async_session, async_aeros_session
|
|
from src.exceptions import handle_exception
|
|
from src.middleware import RequestValidationMiddleware
|
|
from src.context import set_request_id, reset_request_id, get_request_id
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
# we configure the logging level and format
|
|
configure_logging()
|
|
|
|
# we define the exception handlers
|
|
exception_handlers = {Exception: handle_exception}
|
|
|
|
# we create the ASGI for the app
|
|
app = FastAPI(exception_handlers=exception_handlers, openapi_url="", title="LCCA API",
|
|
description="Welcome to RBD's API documentation!",
|
|
version="0.1.0")
|
|
app.state.limiter = limiter
|
|
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
|
app.add_middleware(GZipMiddleware, minimum_size=2000)
|
|
|
|
app.add_middleware(RequestValidationMiddleware)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def db_session_middleware(request: Request, call_next):
|
|
request_id = str(uuid1())
|
|
|
|
# we create a per-request id such that we can ensure that our session is scoped for a particular request.
|
|
# see: https://github.com/tiangolo/fastapi/issues/726
|
|
ctx_token = set_request_id(request_id)
|
|
|
|
try:
|
|
session = async_scoped_session(async_session, scopefunc=get_request_id)
|
|
request.state.db = session()
|
|
|
|
collector_session = async_scoped_session(async_aeros_session, scopefunc=get_request_id)
|
|
request.state.aeros_db = collector_session()
|
|
|
|
start_time = time.time()
|
|
response = await call_next(request)
|
|
process_time = (time.time() - start_time) * 1000
|
|
|
|
log.info(
|
|
f"Request: {request.method} {request.url.path} Status: {response.status_code} Duration: {process_time:.2f}ms"
|
|
)
|
|
|
|
except Exception as e:
|
|
log.error(f"Request failed: {request.method} {request.url.path} Error: {str(e)}")
|
|
raise e from None
|
|
finally:
|
|
await request.state.db.close()
|
|
await request.state.aeros_db.close()
|
|
|
|
reset_request_id(ctx_token)
|
|
return response
|
|
|
|
|
|
@app.middleware("http")
|
|
async def add_security_headers(request: Request, call_next):
|
|
response = await call_next(request)
|
|
response.headers["Strict-Transport-Security"] = "max-age=31536000 ; includeSubDomains"
|
|
return response
|
|
|
|
|
|
app.mount("/model", StaticFiles(directory="model"), name="model")
|
|
|
|
@app.get("/images/{image_path:path}")
|
|
async def get_image(image_path: str = Path(...)):
|
|
# Extract filename from the full path
|
|
filename = os.path.basename(image_path)
|
|
full_image_path = f"model/RBD Model/Image/{filename}"
|
|
|
|
if os.path.exists(full_image_path):
|
|
return FileResponse(full_image_path)
|
|
else:
|
|
raise HTTPException(status_code=404, detail="Image not found")
|
|
|
|
app.include_router(api_router)
|