Files
webref/backend/app/main.py
Danilo Reyes b55ac51fe2 feat: add unified linting scripts and git hooks for code quality enforcement
- Introduced `lint` and `lint-fix` applications in `flake.nix` for unified linting of backend (Python) and frontend (TypeScript/Svelte) code.
- Added `scripts/lint.sh` for manual linting execution.
- Created `scripts/install-hooks.sh` to set up git hooks for automatic linting before commits and optional tests before pushes.
- Updated `README.md` with instructions for using the new linting features and git hooks.
2025-11-02 00:08:37 -06:00

104 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
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}")
# Additional routers will be added in subsequent phases
# from app.api import boards, images
# app.include_router(boards.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}")