Skip to content

Commit

Permalink
Move demo API routes to independent module
Browse files Browse the repository at this point in the history
  • Loading branch information
boholder committed Mar 1, 2024
1 parent 006cdc6 commit f186a66
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
31 changes: 6 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
from asgi_correlation_id import correlation_id, CorrelationIdMiddleware
from fastapi import FastAPI, HTTPException, Request
from fastapi.exception_handlers import http_exception_handler
from pydantic import BaseModel
from starlette.responses import Response
from uvicorn.config import LOGGING_CONFIG

import log_config
from routers import demo

log: logging.Logger


class Item(BaseModel):
name: str
price: float


FILE_HANDLER_NAME = "file_handler"


Expand All @@ -43,27 +37,10 @@ async def lifespan(app: FastAPI):


app = FastAPI(lifespan=lifespan)
app.include_router(demo.router)
app.add_middleware(CorrelationIdMiddleware)


@app.get("/")
def read_root():
log.info("info log")
log.debug("debug log")
log.error("error log")
return {"Hello": "World"}


@app.get("/get/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}


@app.post("/post")
def post_item(item: Item):
return {"item_name": item.name, "item_price": item.price}


@app.exception_handler(Exception)
async def unhandled_exception_handler(request: Request, exc: Exception) -> Response:
"""
Expand All @@ -79,13 +56,17 @@ async def unhandled_exception_handler(request: Request, exc: Exception) -> Respo


def configure_uvicorn_logging():
"""Must configure the uvicorn.config.LOGGING_CONFIG within the same module that runs uvicorn.run()."""
extra_filters = [log_config.TRACE_ID_FILTER]
for handler in LOGGING_CONFIG["handlers"].values():
handler["filters"] = handler["filters"] + extra_filters if "filters" in handler else extra_filters
for formatter in LOGGING_CONFIG["formatters"].values():
formatter["fmt"] = log_config.LOG_FORMAT_PATTERN

LOGGING_CONFIG["handlers"][FILE_HANDLER_NAME] = log_config.FILE_HANDLER_CONFIG
# There are three loggers in uvicorn default logging config: "uvicorn", "uvicorn.access", "uvicorn.error".
# We need to add "file_handler" to logger "uvicorn" and "uvicorn.access"
# since logger "uvicorn.error" will propagate the log to the other two.
for logger in LOGGING_CONFIG["loggers"].values():
if "handlers" in logger:
logger["handlers"] += [FILE_HANDLER_NAME]
Expand Down
Empty file added app/routers/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions app/routers/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import logging

from fastapi import APIRouter
from pydantic import BaseModel

log = logging.getLogger(__name__)
router = APIRouter()


class Item(BaseModel):
name: str
price: float


@router.get("/")
def read_root():
log.info("info log")
log.debug("debug log")
log.error("error log")
return {"Hello": "World"}


@router.get("/get/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}


@router.post("/post")
def post_item(item: Item):
return {"item_name": item.name, "item_price": item.price}

0 comments on commit f186a66

Please sign in to comment.