Files
webref/backend/app/main.py
Danilo Reyes 948fe591dc
All checks were successful
CI/CD Pipeline / VM Test - security (push) Successful in 7s
CI/CD Pipeline / Backend Linting (push) Successful in 4s
CI/CD Pipeline / VM Test - backend-integration (push) Successful in 11s
CI/CD Pipeline / VM Test - full-stack (push) Successful in 8s
CI/CD Pipeline / VM Test - performance (push) Successful in 8s
CI/CD Pipeline / Nix Flake Check (push) Successful in 38s
CI/CD Pipeline / CI Summary (push) Successful in 0s
CI/CD Pipeline / Frontend Linting (push) Successful in 17s
phase 13
2025-11-02 14:48:03 -06:00

103 lines
2.8 KiB
Python

"""FastAPI application entry point."""
import logging
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from app.api import auth, boards, groups, images
from app.core.config import settings
from app.core.errors import WebRefException
from app.core.logging import setup_logging
from app.core.middleware import setup_middleware
# Setup logging
setup_logging()
logger = logging.getLogger(__name__)
# Create FastAPI application
app = FastAPI(
title=settings.APP_NAME,
version=settings.APP_VERSION,
description="Reference Board Viewer - Web-based visual reference management",
docs_url="/docs",
redoc_url="/redoc",
openapi_url=f"{settings.API_V1_PREFIX}/openapi.json",
)
# Setup middleware
setup_middleware(app)
# Exception handlers
@app.exception_handler(WebRefException)
async def webref_exception_handler(request: Request, exc: WebRefException):
"""Handle custom WebRef exceptions."""
logger.error(f"WebRef exception: {exc.message}", extra={"details": exc.details})
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.message,
"details": exc.details,
"status_code": exc.status_code,
},
)
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
"""Handle unexpected exceptions."""
logger.exception("Unexpected error occurred")
return JSONResponse(
status_code=500,
content={
"error": "Internal server error",
"details": str(exc) if settings.DEBUG else {},
"status_code": 500,
},
)
# Health check endpoint
@app.get("/health", tags=["System"])
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"version": settings.APP_VERSION,
"app": settings.APP_NAME,
}
# Root endpoint
@app.get("/", tags=["System"])
async def root():
"""Root endpoint with API information."""
return {
"message": f"Welcome to {settings.APP_NAME} API",
"version": settings.APP_VERSION,
"docs": "/docs",
"health": "/health",
}
# API routers
app.include_router(auth.router, prefix=f"{settings.API_V1_PREFIX}")
app.include_router(boards.router, prefix=f"{settings.API_V1_PREFIX}")
app.include_router(groups.router, prefix=f"{settings.API_V1_PREFIX}")
app.include_router(images.router, prefix=f"{settings.API_V1_PREFIX}")
@app.on_event("startup")
async def startup_event():
"""Application startup tasks."""
logger.info(f"Starting {settings.APP_NAME} v{settings.APP_VERSION}")
logger.info(f"Debug mode: {settings.DEBUG}")
logger.info(f"API prefix: {settings.API_V1_PREFIX}")
@app.on_event("shutdown")
async def shutdown_event():
"""Application shutdown tasks."""
logger.info(f"Shutting down {settings.APP_NAME}")