Middleware for FastAPI
Project description
FastAPI middleware
Introduction
fastapi-middleware is a set of middlewares for FastAPI framework.
Installation
pip install fastapi-middleware
Usage
To use middleware, you need to import it and add to your FastAPI app:
from fastapi import FastAPI
...
from fastapi_middleware import (
GlobalContextMiddleware,
global_ctx,
SQLQueriesMiddleware,
)
...
app = FastAPI()
...
# set desired logging level
logging.getLogger("fastapi-middleware").setLevel(logging.DEBUG)
# add desired middleware
app.add_middleware(SQLQueriesMiddleware)
app.add_middleware(GlobalContextMiddleware)
@app.get("/test")
def get_test():
global_ctx.foo = 'bar'
return {'ok': True}
GlobalContextMiddleware gives you a global context object (global_ctx) to store data during the lifetime of the request. You can add request-specific data, for example, in other middleware and use it across application.
SQLQueriesMiddleware would log insights about SQL queries made with SQLAlchemy like total number of queries, total duration, slowest/fastest queries, etc.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_middleware-0.2.0-py3-none-any.whl
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | dacce9c9ab913da574a07b32b5fc6f67fe9e07c493adc4c054102a8e12d9b843 |
|
| MD5 | 9cea3037c2bea8c36ba95918ea8f32e5 |
|
| BLAKE2b-256 | 0ddd854aeeb9230f4b13845c2dc7f843087149591c79bc30f906216f088cf5b0 |