agent-verif / packages /api /src /main.py
claude's playground
feat(agent verf): Add multi-provider LLM infrastructure with dual-mode support
0a6602d
Raw
History Blame Contribute Delete
4.18 kB
# ============================================================================
# agent verf - FastAPI Application
# Version: 0.1.0
# Last Updated: 2026-01-13
#
# Main FastAPI application entry point.
#
# Usage:
# uvicorn src.main:app --reload --port 8000
#
# Endpoints:
# POST /api/v1/verify - Submit URL for verification
# GET /api/v1/status/:id - Check verification status
# GET /api/v1/receipt/:id - Get verification receipt
# GET /health - Health check
# ============================================================================
import os
import structlog
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from src.routes import verify, health, receipts, user
# Load environment variables
load_dotenv()
# Configure structured logging
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.stdlib.BoundLogger,
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
logger = structlog.get_logger()
# ============================================================================
# Lifespan Events
# ============================================================================
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Startup and shutdown events for the application.
Startup:
- Initialize Redis connection pool
- Warm up LLM client
- Log startup
Shutdown:
- Close Redis connections
- Log shutdown
"""
# Startup
logger.info(
"Starting agent verf API",
version="0.1.0",
environment=os.getenv("ENVIRONMENT", "development"),
)
# TODO: Initialize Redis pool
# TODO: Warm up LLM client
yield
# Shutdown
logger.info("Shutting down agent verf API")
# ============================================================================
# Application Factory
# ============================================================================
def create_app() -> FastAPI:
"""
Create and configure the FastAPI application.
Returns:
Configured FastAPI app instance
"""
app = FastAPI(
title="agent verf API",
description="Social media fact-checking and verification API",
version="0.1.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan,
)
# Configure CORS
# In production, restrict origins to your frontend domain
allowed_origins = os.getenv("CORS_ORIGINS", "http://localhost:3000").split(",")
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PATCH", "OPTIONS"],
allow_headers=["*"],
)
# Register routes
app.include_router(health.router, tags=["Health"])
app.include_router(verify.router, prefix="/api/v1", tags=["Verification"])
app.include_router(receipts.router, prefix="/api/v1", tags=["Receipts"])
app.include_router(user.router, prefix="/api/v1/user", tags=["User"])
return app
# ============================================================================
# App Instance
# ============================================================================
app = create_app()
# ============================================================================
# Root Route
# ============================================================================
@app.get("/")
async def root():
"""
Root endpoint - API info.
"""
return {
"name": "agent verf API",
"version": "0.1.0",
"docs": "/docs",
"health": "/health",
}