跳转到主要内容

基于Werkzeug项目的DispatcherMiddleware的多应用程序分发器。

项目描述

Sanic-Dispatcher

Sanic的分配器扩展,同时作为Sanic-to-WSGI适配器

允许你这样做:(认真地说)

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)

安装

pip install Sanic-Dispatcher

如何使用

首先按照正常方式创建一个Sanic应用程序

from sanic import Sanic

app = Sanic(__name__) # This creates a sanic app

app成为你的'基础'或'父'Sanic应用程序,该应用程序将容纳分配器扩展

创建一个分配器

from sanic_dispatcher import SanicDispatcherMiddlewareController

dispatcher = SanicDispatcherMiddlewareController(app)

dispatcher是你的新分配器控制器。注意:这将其第一个参数作为对app的引用,但它不消耗app,也不返回app

我想分配另一个Sanic应用程序

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")

浏览到url /childprefix/将调用otherapp应用程序,并调用显示“来自子应用程序的Hello World”的/路由

如果其他应用程序是Flask应用程序怎么办?

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")

浏览到url /flaskprefix/将调用Flask应用程序,并调用显示“来自Flask应用程序的Hello World”的/路由

如果其他应用程序是Django应用程序怎么办?

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")

浏览到url /djangoprefix/将调用Django应用程序。

我可以运行默认应用程序吗?

您在开始时创建的Sanic应用程序app也是默认应用程序。

当您导航到不匹配已注册分配前缀的URL时,此Sanic应用程序将按正常方式自行处理请求。

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")

浏览到url /不会调用任何分配器应用程序,因此app将自行处理请求,解析显示“来自默认应用程序的Hello World”的/路由

我想将通用中间件应用于注册的应用程序!

简单!

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)

关键在于将 apply_middleware=True 传递给相关注册应用程序函数。默认情况下,所有注册的分派应用程序的 apply_middleware 都设置为 False

在这个示例中,Sanic 请求中间件 modify_request 将应用于所有请求,包括由分派器上注册的应用程序处理的请求。请求中间件将在将其传递给任何注册应用程序之前应用于 request

在这个示例中,Sanic 响应中间件 modify_response 将应用于所有响应,包括由分派器上注册的应用程序生成的响应。响应中间件将在注册应用程序处理响应之后应用于 response

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源代码分发

Sanic-Dispatcher-0.8.0.0.tar.gz (11.0 kB 查看哈希值)

上传时间 源代码

构建分发

Sanic_Dispatcher-0.8.0.0-py2.py3-none-any.whl (10.8 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面