Skip to content

kholodnyi/fastapi-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages