refactor: remove all inline comments from code
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
"""Common error response schema and exception handlers."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
@@ -11,8 +9,6 @@ from app.core.exceptions import AppException
|
||||
|
||||
|
||||
class ErrorResponse(BaseModel):
|
||||
"""Standard error response format."""
|
||||
|
||||
status_code: int
|
||||
message: str
|
||||
details: dict | None = None
|
||||
@@ -20,7 +16,6 @@ class ErrorResponse(BaseModel):
|
||||
|
||||
|
||||
async def app_exception_handler(request: Request, exc: AppException) -> JSONResponse:
|
||||
"""Handle application exceptions with standard response."""
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={
|
||||
@@ -32,7 +27,6 @@ async def app_exception_handler(request: Request, exc: AppException) -> JSONResp
|
||||
|
||||
|
||||
async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
|
||||
"""Handle HTTP exceptions with standard response."""
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={
|
||||
@@ -44,7 +38,6 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> JSONRe
|
||||
|
||||
|
||||
def register_exception_handlers(app: FastAPI):
|
||||
"""Register all exception handlers with FastAPI app."""
|
||||
app.add_exception_handler(
|
||||
AppException,
|
||||
app_exception_handler, # type: ignore[arg-type]
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
"""Application configuration and settings."""
|
||||
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Application settings from environment variables."""
|
||||
|
||||
app_name: str = "Blog API"
|
||||
debug: bool = False
|
||||
host: str = "0.0.0.0"
|
||||
port: int = 8000
|
||||
|
||||
# Database (when added)
|
||||
database_url: str | None = None
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env")
|
||||
|
||||
@@ -1,64 +1,25 @@
|
||||
"""Custom application exceptions."""
|
||||
|
||||
|
||||
class AppException(Exception):
|
||||
"""Base application exception."""
|
||||
|
||||
def __init__(self, message: str, status_code: int = 500):
|
||||
"""Initialize application exception.
|
||||
|
||||
Args:
|
||||
message: Error message.
|
||||
status_code: HTTP status code.
|
||||
"""
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class NotFoundError(AppException):
|
||||
"""Resource not found error."""
|
||||
|
||||
def __init__(self, message: str = "Resource not found"):
|
||||
"""Initialize not found error.
|
||||
|
||||
Args:
|
||||
message: Error message.
|
||||
"""
|
||||
super().__init__(message, status_code=404)
|
||||
|
||||
|
||||
class ValidationError(AppException):
|
||||
"""Validation error."""
|
||||
|
||||
def __init__(self, message: str = "Validation failed"):
|
||||
"""Initialize validation error.
|
||||
|
||||
Args:
|
||||
message: Error message.
|
||||
"""
|
||||
super().__init__(message, status_code=400)
|
||||
|
||||
|
||||
class UnauthorizedError(AppException):
|
||||
"""Authentication required."""
|
||||
|
||||
def __init__(self, message: str = "Unauthorized"):
|
||||
"""Initialize unauthorized error.
|
||||
|
||||
Args:
|
||||
message: Error message.
|
||||
"""
|
||||
super().__init__(message, status_code=401)
|
||||
|
||||
|
||||
class ForbiddenError(AppException):
|
||||
"""Permission denied."""
|
||||
|
||||
def __init__(self, message: str = "Forbidden"):
|
||||
"""Initialize forbidden error.
|
||||
|
||||
Args:
|
||||
message: Error message.
|
||||
"""
|
||||
super().__init__(message, status_code=403)
|
||||
|
||||
16
app/main.py
16
app/main.py
@@ -1,5 +1,3 @@
|
||||
"""FastAPI application factory and entry point."""
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
@@ -11,32 +9,18 @@ from app.core.config import settings
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan manager for startup/shutdown events."""
|
||||
# Startup: initialize DB connections, cache, etc.
|
||||
yield
|
||||
# Shutdown: cleanup resources
|
||||
|
||||
|
||||
def app_factory() -> FastAPI:
|
||||
"""Create and configure FastAPI application instance.
|
||||
|
||||
Returns:
|
||||
Configured FastAPI application.
|
||||
"""
|
||||
app = FastAPI(title=settings.app_name, debug=settings.debug, lifespan=lifespan)
|
||||
|
||||
# Register exception handlers
|
||||
register_exception_handlers(app)
|
||||
|
||||
# Register routers (when added)
|
||||
# from app.api.v1.router import api_router
|
||||
# app.include_router(api_router, prefix="/api/v1")
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def main():
|
||||
"""Run the application with uvicorn server."""
|
||||
uvicorn.run(app_factory, factory=True, host=settings.host, port=settings.port)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user