Skip to main content

Multi-application dispatcher based on DispatcherMiddleware from the Werkzeug Project.

Project description

#Sanic-Dispatcher
###A Dispatcher extension for Sanic that also acts as a Sanic-to-WSGI adapter
#
Allows you to do this: *(seriously)*
```python
from sanic import Sanic, response
from sanic_dispatcher import SanicDispatcherMiddlewareController
from flask import Flask, make_response, current_app as flask_app

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)

child_sanic_app = Sanic("MyChildSanicApp")

child_flask_app = Flask("MyChildFlaskApp")

@app.middleware("response")
async def modify_response(request, response):
response.body = response.body + b"\nModified by Sanic Response middleware!"
response.headers['Content-Length'] = len(response.body)
return response

@app.route("/")
async def index(request):
return response.text("Hello World from {}".format(request.app.name))

@child_sanic_app.route("/")
async def index(request):
return response.text("Hello World from {}".format(request.app.name))

@child_flask_app.route("/")
def index():
app = flask_app
return make_response("Hello World from {}".format(app.import_name))

dispatcher.register_sanic_application(child_sanic_app, '/sanicchild', apply_middleware=True)
dispatcher.register_wsgi_application(child_flask_app.wsgi_app, '/flaskchild', apply_middleware=True)

if __name__ == "__main__":
app.run(port=8001, debug=True)
```
#
##How To Use
First make a Sanic application the way you normally do:
```python
from sanic import Sanic

app = Sanic(__name__) # This creates a sanic app
```
`app` becomes your 'base' or 'parent' sanic app which will accommodate the Dispatcher extension

Create a Dispatcher
```python
from sanic_dispatcher import SanicDispatcherMiddlewareController

dispatcher = SanicDispatcherMiddlewareController(app)
```
`dispatcher` is your new dispatcher controller.
*Note: This takes a reference to `app` as its first parameter, but it does not consume `app`, nor does it return `app`.*

**I want to dispatch another Sanic App**
```python
app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)

otherapp = Sanic("MyChildApp")

dispatcher.register_sanic_application(otherapp, "/childprefix")

@otherapp.route('/')
async def index(request):
return response.text("Hello World from Child App")
```
Browsing to url `/childprefix/` will invoke the `otherapp` App, and call the `/` route which displays "Hello World from Child App"

**What if the other App is a Flask App?**
```python
from flask import Flask, make_response

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)
flaskapp = Flask("MyFlaskApp")

# register the wsgi_app method from the flask app into the dispatcher
dispatcher.register_wsgi_application(flaskapp.wsgi_app, "/flaskprefix")

@flaskapp.route('/')
def index():
return make_response("Hello World from Flask App")
```
Browsing to url `/flaskprefix/` will invoke the Flask App, and call the `/` route which displays "Hello World from Flask App"

**What if the other App is a Django App?**
```python
import my_django_app

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)
# register the django wsgi application into the dispatcher
dispatcher.register_wsgi_application(my_django_app.wsgi.application,
"/djangoprefix")
```
Browsing to url `/djangoprefix/` will invoke the Django App.

**Can I run a default application?**

The Sanic App `app` you create at the start is also the default app.

When you navigate to a URL that does not match a registered dispatch prefix, this Sanic app will handle the request itself as per normal.
```python
app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)

otherapp = Sanic("MyChildApp")

dispatcher.register_sanic_application(otherapp, "/childprefix")

@app.route('/')
async def index(request):
return response.text("Hello World from Default App")

@otherapp.route('/')
async def index(request):
return response.text("Hello World from Child App")
```
Browsing to url `/` will *not* invoke any Dispatcher applications, so `app` will handle the request itself, resolving the `/` route which displays "Hello World from Default App"

**I want to apply common middleware to the registered applications!**

Easy!
```python
import my_django_app
from flask import Flask, make_response, current_app

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)

child_sanic_app = Sanic("MyChildSanicApp")

child_flask_app = Flask("MyChildFlaskApp")

@app.middleware("request")
async def modify_request(request):
request.headers['Content-Type'] = "text/plain"

@app.middleware("response")
async def modify_response(request, response):
response.body = response.body + b"\nModified by Sanic Response middleware!"
response.headers['Content-Length'] = len(response.body)
return response

@app.route("/")
async def index(request):
return response.text("Hello World from {}".format(request.app.name))

@child_sanic_app.route("/")
async def index(request):
return response.text("Hello World from {}".format(request.app.name))

@child_flask_app.route("/")
def index():
app = current_app
return make_response("Hello World from {}".format(app.import_name))

dispatcher.register_sanic_application(child_sanic_app,
'/childprefix', apply_middleware=True)
dispatcher.register_wsgi_application(my_django_app.wsgi.application,
'/djangoprefix', apply_middleware=True)
dispatcher.register_wsgi_application(child_flask_app.wsgi_app,
'/flaskprefix', apply_middleware=True)
```
The key here is passing `apply_middleware=True` to the relevant register application function. By default `apply_middleware` is set to `False` for all registered dispatcher applications.

In this example the Sanic Request Middleware `modify_request` will be applied to ALL requests, including those handled by applications registered on the dispatcher. The request middleware will be applied to the `request` *before* it is passed to any registered applications.

In this example the Sanic Response Middleware `modify_response` will be applied to ALL responses, including those which were generated by applications registered on the dispatcher. The response middleware will be applied to the `response` *after* it is processed by the registered application.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Sanic-Dispatcher-0.5.2.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distributions

Sanic_Dispatcher-0.5.2.1-py3.5.egg (13.9 kB view details)

Uploaded Source

Sanic_Dispatcher-0.5.2.1-py2.py3-none-any.whl (10.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file Sanic-Dispatcher-0.5.2.1.tar.gz.

File metadata

File hashes

Hashes for Sanic-Dispatcher-0.5.2.1.tar.gz
Algorithm Hash digest
SHA256 a9044605936d7b5b9da3345b95472128ef71b3fd3d3b0cec3a619cdf3b3454e3
MD5 4a3ca909aba374c425dd1db124a8741c
BLAKE2b-256 a90755802d9e2304c13b97dd13b0bc829a92190e786a5d0130485ab897e3262f

See more details on using hashes here.

Provenance

File details

Details for the file Sanic_Dispatcher-0.5.2.1-py3.5.egg.

File metadata

File hashes

Hashes for Sanic_Dispatcher-0.5.2.1-py3.5.egg
Algorithm Hash digest
SHA256 e1fb034b697900a427e0abe3d235024b1b8c6995e583590d6d07484190a9f8bf
MD5 1808d1dd56253c3b28980aa99390fe5f
BLAKE2b-256 f868f573ae15aa558bdda4f1487a47deeeebb8cd96e42e6c21a7240bd2845a95

See more details on using hashes here.

Provenance

File details

Details for the file Sanic_Dispatcher-0.5.2.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Sanic_Dispatcher-0.5.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b6192efc1e08b98ccc59dc1391f6b0641f80407a0e3365611ef4208febe4342b
MD5 9dae77e8511bc9c152fef371b5150e2d
BLAKE2b-256 3c61331cb724bde8c72172a26cd7d05dd2b77a2fbdf62b58c57a7dc5c2c89035

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page