apac-ops-bot/backend/app/main.py

83 lines
1.8 KiB
Python

"""
APAC Ops Bot - FastAPI Application Entry Point
This is the main application file that initializes FastAPI,
configures middleware, and sets up routing.
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from app.config import settings
from app.database import init_db, close_db
# Import routers
from app.api.v1.router import api_router
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan context manager for startup and shutdown events
"""
# Startup
print(f"🚀 Starting {settings.APP_NAME}...")
print(f"📦 Environment: {settings.APP_ENV}")
await init_db()
print("✅ Database initialized")
yield
# Shutdown
print("🔄 Shutting down...")
await close_db()
print("✅ Cleanup complete")
# Initialize FastAPI application
app = FastAPI(
title=settings.APP_NAME,
description="AI-powered operations assistant for Oliver Agency's APAC region",
version="1.0.0",
docs_url="/docs" if settings.DEBUG else None,
redoc_url="/redoc" if settings.DEBUG else None,
lifespan=lifespan,
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Health check endpoint
@app.get("/health")
async def health_check():
"""
Health check endpoint for monitoring
"""
return {
"status": "healthy",
"app": settings.APP_NAME,
"environment": settings.APP_ENV,
}
# Include API routers
app.include_router(api_router, prefix="/api/v1")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8048,
reload=settings.DEBUG,
)