Add signals before and after handler execution (#2540)
This commit is contained in:
10
sanic/app.py
10
sanic/app.py
@@ -894,9 +894,19 @@ class Sanic(BaseSanic, RunnerMixin, metaclass=TouchUpMeta):
|
||||
)
|
||||
|
||||
# Run response handler
|
||||
await self.dispatch(
|
||||
"http.handler.before",
|
||||
inline=True,
|
||||
context={"request": request},
|
||||
)
|
||||
response = handler(request, **request.match_info)
|
||||
if isawaitable(response):
|
||||
response = await response
|
||||
await self.dispatch(
|
||||
"http.handler.after",
|
||||
inline=True,
|
||||
context={"request": request},
|
||||
)
|
||||
|
||||
if request.responded:
|
||||
if response is not None:
|
||||
|
||||
@@ -30,6 +30,8 @@ class Event(Enum):
|
||||
HTTP_LIFECYCLE_RESPONSE = "http.lifecycle.response"
|
||||
HTTP_ROUTING_AFTER = "http.routing.after"
|
||||
HTTP_ROUTING_BEFORE = "http.routing.before"
|
||||
HTTP_HANDLER_AFTER = "http.handler.after"
|
||||
HTTP_HANDLER_BEFORE = "http.handler.before"
|
||||
HTTP_LIFECYCLE_SEND = "http.lifecycle.send"
|
||||
HTTP_MIDDLEWARE_AFTER = "http.middleware.after"
|
||||
HTTP_MIDDLEWARE_BEFORE = "http.middleware.before"
|
||||
@@ -53,6 +55,8 @@ RESERVED_NAMESPACES = {
|
||||
Event.HTTP_LIFECYCLE_RESPONSE.value,
|
||||
Event.HTTP_ROUTING_AFTER.value,
|
||||
Event.HTTP_ROUTING_BEFORE.value,
|
||||
Event.HTTP_HANDLER_AFTER.value,
|
||||
Event.HTTP_HANDLER_BEFORE.value,
|
||||
Event.HTTP_LIFECYCLE_SEND.value,
|
||||
Event.HTTP_MIDDLEWARE_AFTER.value,
|
||||
Event.HTTP_MIDDLEWARE_BEFORE.value,
|
||||
|
||||
Reference in New Issue
Block a user