Middlewares
- Middleware.on_startup()
- Middleware.on_shutdown()
- Middleware.handle_connection()
- Middleware.handle_websocket_message()
- Middleware.handle_request()
Lona middlewares control connection handling, request handling, websocket messages and get called on server start and shutdown.
Besides on_startup() and on_shutdown() middlewares work like input event handlers: The middleware data gets passed into every middleware until the data does not get returned. When the data does not get returned, Lona regards the data as handled.
Middlewares can be live analyzed by using the Lona Shell command %lona_middlewares.
# middlewares/my_middleware.py
class MyMiddleware:
async def on_startup(self, data):
server = data.server
return data
async def on_shutdown(self, data):
server = data.server
return data
def handle_connection(self, data):
server = data.server
http_request = data.http_request
connection = data.connection
return data
def handle_websocket_message(self, data):
server = data.server
connection = data.connection
message = data.message
return data
def handle_request(self, data):
server = data.server
connection = data.connection
request = data.request
view = data.view
return data
# settings.py
MIDDLEWARES = [
'middlewares/my_middleware.py::MyMiddleware'
]
Middleware.handle_connection(data)
Gets called with every new connection that is made. If data is not returned, the connection gets dropped and a 503 error gets sent.
Middleware.handle_request(data)
Gets called for every request is made by any user.
If the data gets returned, the view associated with this request gets started.
If a Response Object is returned, the view gets not started and the user gets the returned response object shown.