forked from Greyisheep/apiconf-agent
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (87 loc) · 2.77 KB
/
main.py
File metadata and controls
101 lines (87 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Main FastAPI application for the API Conference AI Agent.
"""
import time
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import uvicorn
from app.config.logger import Logger
from app.config.settings import settings
from app.api.v1.agents_router import router as agents_router
# Setup logging
Logger.setup_root_logger()
logger = Logger.get_logger(__name__)
# Global startup time
startup_time = time.time()
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan manager."""
# Startup
logger.info("Starting API Conference AI Agent...")
logger.info(f"Environment: {settings.environment}")
logger.info(f"Model: {settings.google_model_name}")
logger.info(f"Venue: {settings.conference_venue_name}")
yield
# Shutdown
logger.info("Shutting down API Conference AI Agent...")
# Create FastAPI app
app = FastAPI(
title="API Conference AI Agent",
description="AI Assistant for the API Conference community in Nigeria",
version="0.1.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(agents_router, prefix="/api/v1/agents", tags=["agents"])
@app.get("/")
async def root():
"""Root endpoint."""
return {
"message": "Welcome to API Conference AI Agent! 🎤",
"version": "0.1.0",
"docs": "/docs",
"health": "/api/v1/agents/health",
"support": settings.support_phone
}
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Global exception handler."""
logger.error(f"Unhandled exception: {exc}")
return JSONResponse(
status_code=500,
content={
"success": False,
"error": "Internal server error",
"message": f"Please contact {settings.support_phone} for assistance.",
"support_contact": settings.support_phone
}
)
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
"""Add processing time header to responses."""
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
if __name__ == "__main__":
logger.info("Starting server...")
uvicorn.run(
"main:app",
host=settings.api_host,
port=settings.api_port,
reload=settings.debug,
log_level=settings.log_level.lower()
)