root ae72b180e5 fix: improve error handling and validation
- Add centralized exception classes
- Validate signature request status before signing
- Check contract status before approval/rejection
- Add exception handlers to FastAPI app
- Update tests for new validation logic

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 03:38:20 +08:00

62 lines
1.4 KiB
Python

"""Main FastAPI application."""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.core.database import init_db
from app.core.exceptions import AppException
from app.core.exception_handlers import app_exception_handler
from app.api.v1 import laws, analyses, contracts, signatures
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan handler."""
# Startup
await init_db()
yield
# Shutdown
pass
app = FastAPI(
title=settings.APP_NAME,
version=settings.APP_VERSION,
lifespan=lifespan,
)
# Register exception handlers
app.add_exception_handler(AppException, app_exception_handler)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(laws.router, prefix="/api/v1")
app.include_router(analyses.router, prefix="/api/v1")
app.include_router(contracts.router, prefix="/api/v1")
app.include_router(signatures.router, prefix="/api/v1")
@app.get("/")
async def root():
"""Root endpoint."""
return {
"name": settings.APP_NAME,
"version": settings.APP_VERSION,
"status": "running"
}
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {"status": "healthy"}