From 62df50e22bcba5cae476b57b4fa175cfea9625f4 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Tue, 10 Jan 2017 18:07:58 -0800 Subject: [PATCH 1/2] add vhosts to blueprints --- examples/vhosts.py | 14 ++++++++++ sanic/blueprints.py | 10 ++++--- tests/test_blueprints.py | 58 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/examples/vhosts.py b/examples/vhosts.py index 50e85494..40dc7ba5 100644 --- a/examples/vhosts.py +++ b/examples/vhosts.py @@ -1,11 +1,15 @@ from sanic.response import text from sanic import Sanic +from sanic.blueprints import Blueprint # Usage # curl -H "Host: example.com" localhost:8000 # curl -H "Host: sub.example.com" localhost:8000 +# curl -H "Host: bp.example.com" localhost:8000/question +# curl -H "Host: bp.example.com" localhost:8000/answer app = Sanic() +bp = Blueprint("bp", host="bp.example.com") @app.route('/', host="example.com") async def hello(request): @@ -14,5 +18,15 @@ async def hello(request): async def hello(request): return text("42") +@bp.route("/question") +async def hello(request): + return text("What is the meaning of life?") + +@bp.route("/answer") +async def hello(request): + return text("42") + +app.register_blueprint(bp) + if __name__ == '__main__': app.run(host="0.0.0.0", port=8000) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 92e376f1..4cf460f3 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -18,14 +18,17 @@ class BlueprintSetup: #: blueprint. self.url_prefix = url_prefix - def add_route(self, handler, uri, methods): + def add_route(self, handler, uri, methods, host=None): """ A helper method to register a handler to the application url routes. """ if self.url_prefix: uri = self.url_prefix + uri - self.app.route(uri=uri, methods=methods)(handler) + if host is None: + host = self.blueprint.host + + self.app.route(uri=uri, methods=methods, host=host)(handler) def add_exception(self, handler, *args, **kwargs): """ @@ -53,7 +56,7 @@ class BlueprintSetup: class Blueprint: - def __init__(self, name, url_prefix=None): + def __init__(self, name, url_prefix=None, host=None): """ Creates a new blueprint :param name: Unique name of the blueprint @@ -63,6 +66,7 @@ class Blueprint: self.url_prefix = url_prefix self.deferred_functions = [] self.listeners = defaultdict(list) + self.host = host def record(self, func): """ diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index f7b9b8ef..04c5c59d 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -59,6 +59,62 @@ def test_several_bp_with_url_prefix(): request, response = sanic_endpoint_test(app, uri='/test2/') assert response.text == 'Hello2' +def test_bp_with_host(): + app = Sanic('test_bp_host') + bp = Blueprint('test_bp_host', url_prefix='/test1', host="example.com") + + @bp.route('/') + def handler(request): + return text('Hello') + + app.blueprint(bp) + headers = {"Host": "example.com"} + request, response = sanic_endpoint_test(app, uri='/test1/', + headers=headers) + + assert response.text == 'Hello' + + +def test_several_bp_with_host(): + app = Sanic('test_text') + bp = Blueprint('test_text', + url_prefix='/test', + host="example.com") + bp2 = Blueprint('test_text2', + url_prefix='/test', + host="sub.example.com") + + @bp.route('/') + def handler(request): + return text('Hello') + + @bp2.route('/') + def handler2(request): + return text('Hello2') + + @bp2.route('/other/') + def handler2(request): + return text('Hello3') + + + app.blueprint(bp) + app.blueprint(bp2) + + assert bp.host == "example.com" + headers = {"Host": "example.com"} + request, response = sanic_endpoint_test(app, uri='/test/', + headers=headers) + assert response.text == 'Hello' + + assert bp2.host == "sub.example.com" + headers = {"Host": "sub.example.com"} + request, response = sanic_endpoint_test(app, uri='/test/', + headers=headers) + + assert response.text == 'Hello2' + request, response = sanic_endpoint_test(app, uri='/test/other/', + headers=headers) + assert response.text == 'Hello3' def test_bp_middleware(): app = Sanic('test_middleware') @@ -162,4 +218,4 @@ def test_bp_static(): request, response = sanic_endpoint_test(app, uri='/testing.file') assert response.status == 200 - assert response.body == current_file_contents \ No newline at end of file + assert response.body == current_file_contents From 15e4ec7ffb25e33a5dafa5950c18a25062e16425 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Tue, 10 Jan 2017 22:08:15 -0800 Subject: [PATCH 2/2] add ability to override default host in blueprint --- sanic/blueprints.py | 8 ++++---- tests/test_blueprints.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 4cf460f3..583aa244 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -87,18 +87,18 @@ class Blueprint: for deferred in self.deferred_functions: deferred(state) - def route(self, uri, methods=None): + def route(self, uri, methods=None, host=None): """ """ def decorator(handler): - self.record(lambda s: s.add_route(handler, uri, methods)) + self.record(lambda s: s.add_route(handler, uri, methods, host)) return handler return decorator - def add_route(self, handler, uri, methods=None): + def add_route(self, handler, uri, methods=None, host=None): """ """ - self.record(lambda s: s.add_route(handler, uri, methods)) + self.record(lambda s: s.add_route(handler, uri, methods, host)) return handler def listener(self, event): diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 04c5c59d..75109e2c 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -67,13 +67,22 @@ def test_bp_with_host(): def handler(request): return text('Hello') + @bp.route('/', host="sub.example.com") + def handler(request): + return text('Hello subdomain!') + app.blueprint(bp) headers = {"Host": "example.com"} request, response = sanic_endpoint_test(app, uri='/test1/', headers=headers) - assert response.text == 'Hello' + headers = {"Host": "sub.example.com"} + request, response = sanic_endpoint_test(app, uri='/test1/', + headers=headers) + + assert response.text == 'Hello subdomain!' + def test_several_bp_with_host(): app = Sanic('test_text')