Initial Python rewrite
This commit is contained in:
79
src/capa_de_integracion/main.py
Normal file
79
src/capa_de_integracion/main.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .config import settings
|
||||
from .dependencies import init_services, shutdown_services, startup_services
|
||||
from .routers import conversation_router, notification_router, quick_replies_router
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
"""Application lifespan manager."""
|
||||
# Startup
|
||||
logger.info("Initializing services...")
|
||||
init_services(settings)
|
||||
await startup_services()
|
||||
logger.info("Application started successfully")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logger.info("Shutting down services...")
|
||||
await shutdown_services()
|
||||
logger.info("Application shutdown complete")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Capa de Integración - Orchestrator Service",
|
||||
description="Conversational AI orchestrator for Dialogflow CX, Gemini, and Vertex AI",
|
||||
version="0.1.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
# CORS middleware
|
||||
# Note: Type checker reports false positive for CORSMiddleware
|
||||
# This is the correct FastAPI pattern per official documentation
|
||||
app.add_middleware(
|
||||
CORSMiddleware, # ty: ignore
|
||||
allow_origins=["*"], # Configure appropriately for production
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Register routers
|
||||
app.include_router(conversation_router)
|
||||
app.include_router(notification_router)
|
||||
app.include_router(quick_replies_router)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint."""
|
||||
return {"status": "healthy", "service": "capa-de-integracion"}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Entry point for CLI."""
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(
|
||||
"capa_de_integracion.main:app",
|
||||
host="0.0.0.0",
|
||||
port=8080,
|
||||
reload=True,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user