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
File details
Details for the file fastapi_middleware-0.2.0.tar.gz
.
File metadata
- Download URL: fastapi_middleware-0.2.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f580723f167469c2abf29c432ebfa2df53ecedf701ecf20500c090c77f002d9 |
|
MD5 | c2c017afe0460148230f28a57d47a3ff |
|
BLAKE2b-256 | f74b77fbca363e6900256e424915e5167f679e73f51d93b12786d949d618842c |
File details
Details for the file fastapi_middleware-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_middleware-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dacce9c9ab913da574a07b32b5fc6f67fe9e07c493adc4c054102a8e12d9b843 |
|
MD5 | 9cea3037c2bea8c36ba95918ea8f32e5 |
|
BLAKE2b-256 | 0ddd854aeeb9230f4b13845c2dc7f843087149591c79bc30f906216f088cf5b0 |