Middlewares in Panther#
Middlewares allow you to process requests and responses globally or for all or specific APIs. They are useful for tasks such as logging, timing, and other cross-cutting concerns.
Global Middlewares#
To apply middlewares globally, define a MIDDLEWARES list in your configs for HTTP middlewares, and a WS_MIDDLEWARE list for WebSocket middlewares. Only HTTP-related middlewares should be placed in MIDDLEWARES, and WebSocket middlewares must be placed in WS_MIDDLEWARE.
Note: Previously, WebSocket middlewares were also defined in
MIDDLEWARES. Now, you must define them inWS_MIDDLEWAREinstead.
Each item can be either a string (dotted path to the middleware class) or the class itself (useful for single-file applications):
class Middleware(HTTPMiddleware):
pass
MIDDLEWARES = [
'core.middlewares.MyMiddleware', # Import by dotted path
Middleware, # Or directly by class
]
WS_MIDDLEWARE = [
'core.middlewares.MyWebsocketMiddleware', # WebSocket middleware by dotted path
MyWebsocketMiddleware, # Or directly by class
]
Per-API Middlewares#
You can assign middlewares to specific APIs, either class-based or function-based:
| apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Middleware Execution Order#
Middlewares are executed in the following order:
- Global middlewares:
__call__()(enter) - Per-API middlewares:
__call__()(enter) - Per-API middlewares:
dispatch(return) - Global middlewares:
dispatch(return)
Creating Custom Middlewares#
Types of Middlewares#
Panther provides two types of middleware base classes:
HTTPMiddleware: For HTTP requests onlyWebsocketMiddleware: For WebSocket connections only
Make sure to inherit from the correct base class:
# For HTTP requests
from panther.middlewares import HTTPMiddleware
# For WebSocket connections
from panther.middlewares import WebsocketMiddleware
Implementing a Middleware#
- Create a class inheriting from
HTTPMiddlewareorWebsocketMiddleware. - Implement an asynchronous
__call__method. - Always return either
await self.dispatch(...)or aResponse/GenericWebsocketinstance at the end of__call__().
Example: HTTP Middleware#
| middlewares.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
Example: WebSocket Middleware#
| middlewares.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 | |
Built-in Middlewares#
Panther provides several built-in middlewares to help with common tasks. Below are the available options and how to use them:
CORS Middleware#
- Purpose: Enables Cross-Origin Resource Sharing (CORS) for your APIs.
- Usage: Add
panther.middlewares.CORSMiddlewareto your globalMIDDLEWARESlist. - Configuration: Requires specific global settings. See the CORS Middleware documentation for configuration details.
Monitoring Middleware#
- Purpose: Logs request and connection data for monitoring and analytics.
- Usage: Add
panther.middlewares.MonitoringMiddlewareto your globalMIDDLEWARESlist. - Note: This middleware or
WebsocketMonitoringMiddlewareis required if you want to use thepanther monitorcommand.
WebSocket Monitoring Middleware#
- Purpose: Similar to
MonitoringMiddleware, but specifically logs data for WebSocket connections. - Usage: Add
panther.middlewares.WebsocketMonitoringMiddlewareto your globalMIDDLEWARESlist if you want to monitor WebSocket traffic. - Note: This middleware or
MonitoringMiddlewareis required if you want to use thepanther monitorcommand.
Tips#
- Use global middlewares for logic that should apply to all requests.
- Use per-API middlewares for logic specific to certain endpoints.
- Always ensure your
__call__method is asynchronous and returns the appropriate value.