2016-10-15 20:38:12 +01:00
|
|
|
# Blueprints
|
|
|
|
|
|
|
|
Blueprints are objects that can be used for sub-routing within an application.
|
2017-01-20 03:18:52 +00:00
|
|
|
Instead of adding routes to the application instance, blueprints define similar
|
2016-10-15 20:38:12 +01:00
|
|
|
methods for adding routes, which are then registered with the application in a
|
2016-10-16 16:35:45 +01:00
|
|
|
flexible and pluggable manner.
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
Blueprints are especially useful for larger applications, where your
|
|
|
|
application logic can be broken down into several groups or areas of
|
|
|
|
responsibility.
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
## My First Blueprint
|
|
|
|
|
|
|
|
The following shows a very simple blueprint that registers a handler-function at
|
|
|
|
the root `/` of your application.
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
Suppose you save this file as `my_blueprint.py`, which can be imported into your
|
2016-10-15 20:38:12 +01:00
|
|
|
main application later.
|
|
|
|
|
|
|
|
```python
|
|
|
|
from sanic.response import json
|
|
|
|
from sanic import Blueprint
|
|
|
|
|
|
|
|
bp = Blueprint('my_blueprint')
|
|
|
|
|
|
|
|
@bp.route('/')
|
2016-10-22 06:13:14 +01:00
|
|
|
async def bp_root(request):
|
2016-10-15 20:38:12 +01:00
|
|
|
return json({'my': 'blueprint'})
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
## Registering blueprints
|
|
|
|
|
2016-10-15 20:38:12 +01:00
|
|
|
Blueprints must be registered with the application.
|
|
|
|
|
|
|
|
```python
|
|
|
|
from sanic import Sanic
|
|
|
|
from my_blueprint import bp
|
|
|
|
|
|
|
|
app = Sanic(__name__)
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
app.run(host='0.0.0.0', port=8000, debug=True)
|
|
|
|
```
|
|
|
|
|
|
|
|
This will add the blueprint to the application and register any routes defined
|
2017-01-20 03:18:52 +00:00
|
|
|
by that blueprint. In this example, the registered routes in the `app.router`
|
|
|
|
will look like:
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
```python
|
|
|
|
[Route(handler=<function bp_root at 0x7f908382f9d8>, methods=None, pattern=re.compile('^/$'), parameters=[])]
|
|
|
|
```
|
2016-10-16 09:59:04 +01:00
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
## Using blueprints
|
|
|
|
|
|
|
|
Blueprints have much the same functionality as an application instance.
|
|
|
|
|
|
|
|
### Middleware
|
|
|
|
|
2016-10-16 22:28:42 +01:00
|
|
|
Using blueprints allows you to also register middleware globally.
|
2016-10-16 09:59:04 +01:00
|
|
|
|
|
|
|
```python
|
|
|
|
@bp.middleware
|
|
|
|
async def halt_request(request):
|
|
|
|
print("I am a spy")
|
|
|
|
|
|
|
|
@bp.middleware('request')
|
|
|
|
async def halt_request(request):
|
|
|
|
return text('I halted the request')
|
|
|
|
|
|
|
|
@bp.middleware('response')
|
|
|
|
async def halt_response(request, response):
|
|
|
|
return text('I halted the response')
|
|
|
|
```
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
### Exceptions
|
|
|
|
|
|
|
|
Exceptions can be applied exclusively to blueprints globally.
|
2016-10-16 09:59:04 +01:00
|
|
|
|
|
|
|
```python
|
|
|
|
@bp.exception(NotFound)
|
|
|
|
def ignore_404s(request, exception):
|
|
|
|
return text("Yep, I totally found the page: {}".format(request.url))
|
2017-01-20 03:18:52 +00:00
|
|
|
```
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
### Static files
|
|
|
|
|
|
|
|
Static files can be served globally, under the blueprint prefix.
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
```python
|
|
|
|
bp.static('/folder/to/serve', '/web/path')
|
2016-10-16 16:35:45 +01:00
|
|
|
```
|
2016-10-21 12:11:18 +01:00
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
## Start and stop
|
|
|
|
|
|
|
|
Blueprints can run functions during the start and stop process of the server.
|
|
|
|
If running in multiprocessor mode (more than 1 worker), these are triggered
|
|
|
|
after the workers fork.
|
|
|
|
|
2016-10-21 12:11:18 +01:00
|
|
|
Available events are:
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
- `before_server_start`: Executed before the server begins to accept connections
|
|
|
|
- `after_server_start`: Executed after the server begins to accept connections
|
|
|
|
- `before_server_stop`: Executed before the server stops accepting connections
|
|
|
|
- `after_server_stop`: Executed after the server is stopped and all requests are complete
|
2016-10-21 12:11:18 +01:00
|
|
|
|
|
|
|
```python
|
|
|
|
bp = Blueprint('my_blueprint')
|
|
|
|
|
2017-01-25 12:01:57 +00:00
|
|
|
@bp.listener('before_server_start')
|
|
|
|
async def setup_connection(app, loop):
|
2016-10-21 12:11:18 +01:00
|
|
|
global database
|
|
|
|
database = mysql.connect(host='127.0.0.1'...)
|
|
|
|
|
2017-01-25 12:01:57 +00:00
|
|
|
@bp.listener('after_server_stop')
|
|
|
|
async def close_connection(app, loop):
|
2016-10-21 12:11:18 +01:00
|
|
|
await database.close()
|
|
|
|
```
|
2017-01-20 03:18:52 +00:00
|
|
|
|
|
|
|
## Use-case: API versioning
|
|
|
|
|
|
|
|
Blueprints can be very useful for API versioning, where one blueprint may point
|
|
|
|
at `/v1/<routes>`, and another pointing at `/v2/<routes>`.
|
|
|
|
|
|
|
|
When a blueprint is initialised, it can take an optional `url_prefix` argument,
|
|
|
|
which will be prepended to all routes defined on the blueprint. This feature
|
|
|
|
can be used to implement our API versioning scheme.
|
|
|
|
|
|
|
|
```python
|
|
|
|
# blueprints.py
|
|
|
|
from sanic.response import text
|
|
|
|
from sanic import Blueprint
|
|
|
|
|
|
|
|
blueprint_v1 = Blueprint('v1')
|
|
|
|
blueprint_v2 = Blueprint('v2')
|
|
|
|
|
|
|
|
@blueprint_v1.route('/')
|
|
|
|
async def api_v1_root(request):
|
|
|
|
return text('Welcome to version 1 of our documentation')
|
|
|
|
|
|
|
|
@blueprint_v2.route('/')
|
|
|
|
async def api_v2_root(request):
|
|
|
|
return text('Welcome to version 2 of our documentation')
|
|
|
|
```
|
|
|
|
|
|
|
|
When we register our blueprints on the app, the routes `/v1` and `/v2` will now
|
|
|
|
point to the individual blueprints, which allows the creation of *sub-sites*
|
|
|
|
for each API version.
|
|
|
|
|
|
|
|
```python
|
|
|
|
# main.py
|
|
|
|
from sanic import Sanic
|
|
|
|
from blueprints import blueprint_v1, blueprint_v2
|
|
|
|
|
|
|
|
app = Sanic(__name__)
|
|
|
|
app.blueprint(blueprint_v1)
|
|
|
|
app.blueprint(blueprint_v2)
|
|
|
|
|
|
|
|
app.run(host='0.0.0.0', port=8000, debug=True)
|
|
|
|
```
|
|
|
|
|
2017-01-20 03:24:08 +00:00
|
|
|
**Previous:** [Exceptions](exceptions.md)
|
2017-01-20 03:18:52 +00:00
|
|
|
|
2017-01-20 03:24:08 +00:00
|
|
|
**Next:** [Class-based views](class_based_views.md)
|