Inject app into request object
Allows for injection of the app object into the request object through
the request handler.
This allows for users to setup things like database connections etc. in
listeners and then utilize them throughout the their various route
handlers.
Usage is fairly simple like so:
```python
@app.get('/')
async def handler(request):
request.app.anything
```
Name is up in the air but I'll leave this up for a few days and I'll
change it if we get a consensus
This commit is contained in:
@@ -360,6 +360,8 @@ class Sanic:
|
||||
# Request Middleware
|
||||
# -------------------------------------------- #
|
||||
|
||||
request.app = self
|
||||
|
||||
response = False
|
||||
# The if improves speed. I don't know why
|
||||
if self.request_middleware:
|
||||
|
||||
@@ -36,7 +36,7 @@ class RequestParameters(dict):
|
||||
class Request(dict):
|
||||
"""Properties of an HTTP request such as URL, headers, etc."""
|
||||
__slots__ = (
|
||||
'url', 'headers', 'version', 'method', '_cookies', 'transport',
|
||||
'app', 'url', 'headers', 'version', 'method', '_cookies', 'transport',
|
||||
'query_string', 'body',
|
||||
'parsed_json', 'parsed_args', 'parsed_form', 'parsed_files',
|
||||
'_ip',
|
||||
@@ -45,6 +45,7 @@ class Request(dict):
|
||||
def __init__(self, url_bytes, headers, version, method, transport):
|
||||
# TODO: Content-Encoding detection
|
||||
url_parsed = parse_url(url_bytes)
|
||||
self.app = None
|
||||
self.url = url_parsed.path.decode('utf-8')
|
||||
self.headers = headers
|
||||
self.version = version
|
||||
|
||||
Reference in New Issue
Block a user