Middleware
A “middleware” is a function that works with every request before it is processed by any specific path operation. And also with every response before returning it.
It takes each request that comes to your application.
It can then do something to that request or run any needed code.
Then it passes the request to be processed by the rest of the application (by some path operation).
It then takes the response generated by the application (by some path operation).
It can do something to that response or run any needed code.
Then it returns the response.
Create a middleware
private_jet = PrivateJet()
class CustomMiddleware:
async def call(self, scope, receive, send):
print("Custom Middleware")
async def app(scope, receive, send):
if scope["type"] == "http":
await private_jet.add_middleware(
CustomMiddleware
)
await private_jet.start(scope=scope, receive=receive, send=send)
Built-in middlewares
CORSMiddleware
from privatejet.middlewares import CORSMiddleware
private_jet = PrivateJet()
async def app(scope, receive, send):
if scope["type"] == "http":
await private_jet.add_middleware(
CORSMiddleware
)
await private_jet.start(scope=scope, receive=receive, send=send)