.
This commit is contained in:
102
src/capa_de_integracion/main.py
Normal file
102
src/capa_de_integracion/main.py
Normal file
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
Copyright 2025 Google. This software is provided as-is, without warranty or
|
||||
representation for any use or purpose. Your use of it is subject to your
|
||||
agreement with Google.
|
||||
|
||||
Main FastAPI application.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .config import get_settings
|
||||
from .controllers import (
|
||||
conversation_router,
|
||||
notification_router,
|
||||
llm_webhook_router,
|
||||
quick_replies_router,
|
||||
data_purge_router,
|
||||
)
|
||||
from .dependencies import init_services, startup_services, shutdown_services
|
||||
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan manager."""
|
||||
# Startup
|
||||
settings = get_settings()
|
||||
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")
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""Create and configure FastAPI application."""
|
||||
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
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
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(llm_webhook_router)
|
||||
app.include_router(quick_replies_router)
|
||||
app.include_router(data_purge_router)
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint."""
|
||||
return {"status": "healthy", "service": "capa-de-integracion"}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
# Create app instance
|
||||
app = create_app()
|
||||
|
||||
|
||||
def main():
|
||||
"""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