ocr-engine-3 / src /app.py
kanha-upadhyay's picture
init
e42e330
import os
from dotenv import load_dotenv
# ===========================
# !!! ATTENTION !!!
# KEEP THIS AT THE TOP TO ENSURE ENVIRONMENT VARIABLES ARE LOADED BEFORE ANY IMPORTS
# ===========================
load_dotenv()
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
from src.controllers import api_router
from src.utils import model_manager
@asynccontextmanager
async def lifespan(app: FastAPI):
try:
logger.info("Starting up the application...")
await model_manager.ensure_models_loaded()
logger.info("Application started successfully...")
yield
except Exception as e:
logger.error(f"Error during startup: {str(e)}")
raise
finally:
logger.info("Application shutdown complete.")
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=[
origin.strip()
for origin in os.getenv(
"CORS_ALLOW_ORIGINS", "http://localhost, http://127.0.0.1"
).split(",")
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def check_health():
return {"response": "Service is healthy!"}
app.include_router(api_router, prefix="/api")