Middlewares
Variable:
MIDDLEWARES
Type:
list
Default:
[]
Structure of middlewares#
MIDDLEWARES
itself is a list
of tuples
which each tuple
is like below:
(Dotted Address of The Middleware Class
, kwargs as dict
)
Custom Middleware#
Middleware Types#
We have 3 type of Middlewares, make sure that you are inheriting from the correct one:
-
Base Middleware
: which is used for bothwebsocket
andhttp
requests -
HTTP Middleware
: which is only used forhttp
requests -
Websocket Middleware
: which is only used forwebsocket
requests
Write Custom Middleware#
-
Write a
class
and inherit from one of the classes below# For HTTP Requests from panther.middlewares.base import HTTPMiddleware # For Websocket Requests from panther.middlewares.base import WebsocketMiddleware # For Both HTTP and Websocket Requests from panther.middlewares.base import BaseMiddleware
-
Then you can write your custom
before()
andafter()
methods -
The
methods
should beasync
before()
should haverequest
parameterafter()
should haveresponse
parameter- overwriting the
before()
andafter()
are optional - The
methods
can getkwargs
from their__init__
Custom HTTP Middleware Example#
-
core/middlewares.py
from panther.middlewares.base import HTTPMiddleware from panther.request import Request from panther.response import Response class CustomMiddleware(HTTPMiddleware): def __init__(self, something): self.something = something async def before(self, request: Request) -> Request: print('Before Endpoint', self.something) return request async def after(self, response: Response) -> Response: print('After Endpoint', self.something) return response
-
core/configs.py
MIDDLEWARES = [ ('core.middlewares.CustomMiddleware', {'something': 'hello-world'}), ]
Custom HTTP + Websocket Middleware Example#
-
core/middlewares.py
from panther.middlewares.base import BaseMiddleware from panther.request import Request from panther.response import Response from panther.websocket import GenericWebsocket class SayHiMiddleware(BaseMiddleware): def __init__(self, name): self.name = name async def before(self, request: Request | GenericWebsocket) -> Request | GenericWebsocket: print('Hello ', self.name) return request async def after(self, response: Response | GenericWebsocket) -> Response | GenericWebsocket: print('Goodbye ', self.name) return response
-
core/configs.py
MIDDLEWARES = [ ('core.middlewares.SayHiMiddleware', {'name': 'Ali Rn'}), ]