基于Werkzeug项目DispatcherMiddleware的多应用程序调度器。
Sanic-Dispatcher的Python项目详细描述
SANIC调度员
sanic的调度程序扩展,它还充当sanic到wsgi的适配器
允许您这样做:(认真地)
fromsanicimportSanic,responsefromsanic_dispatcherimportSanicDispatcherMiddlewareControllerfromflaskimportFlask,make_response,current_appasflask_appapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)child_sanic_app=Sanic("MyChildSanicApp")child_flask_app=Flask("MyChildFlaskApp")@app.middleware("response")asyncdefmodify_response(request,response):response.body=response.body+b"\nModified by Sanic Response middleware!"response.headers['Content-Length']=len(response.body)returnresponse@app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_sanic_app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_flask_app.route("/")defindex():app=flask_appreturnmake_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应用程序:
fromsanicimportSanicapp=Sanic(__name__)# This creates a sanic app
app
成为您的'base'或'parent'Sanic应用程序,它将容纳Dispatcher扩展
创建调度器
fromsanic_dispatcherimportSanicDispatcherMiddlewareControllerdispatcher=SanicDispatcherMiddlewareController(app)
dispatcher
是您的新调度控制器。
注意:这将引用app
作为其第一个参数,但它不使用app
,也不返回app
。
我想发送另一个sanic应用程序
app=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)otherapp=Sanic("MyChildApp")dispatcher.register_sanic_application(otherapp,"/childprefix")@otherapp.route('/')asyncdefindex(request):returnresponse.text("Hello World from Child App")
浏览到url /childprefix/
将调用otherapp
应用程序,并调用显示“hello world from child app”的/
路由
如果另一个应用程序是flask应用程序怎么办?
fromflaskimportFlask,make_responseapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)flaskapp=Flask("MyFlaskApp")# register the wsgi_app method from the flask app into the dispatcherdispatcher.register_wsgi_application(flaskapp.wsgi_app,"/flaskprefix")@flaskapp.route('/')defindex():returnmake_response("Hello World from Flask App")
浏览到url/flaskprefix/
将调用flask应用程序,并调用显示“hello world from flask app”
如果另一个应用程序是django应用程序怎么办?
importmy_django_appapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)# register the django wsgi application into the dispatcherdispatcher.register_wsgi_application(my_django_app.wsgi.application,"/djangoprefix")
浏览到url/djangoprefix/
将调用django应用程序。
我可以运行默认应用程序吗?
您在开始时创建的sanic应用程序也是默认应用程序。
当您导航到与注册的分派前缀不匹配的url时,此sanic应用程序将按照正常方式处理请求本身。
app=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)otherapp=Sanic("MyChildApp")dispatcher.register_sanic_application(otherapp,"/childprefix")@app.route('/')asyncdefindex(request):returnresponse.text("Hello World from Default App")@otherapp.route('/')asyncdefindex(request):returnresponse.text("Hello World from Child App")
浏览到url /
将not调用任何调度程序应用程序,因此app
将处理该请求本身,解析显示“hello world from default app”
我想将公共中间件应用于注册的应用程序!
别紧张!
importmy_django_appfromflaskimportFlask,make_response,current_appapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)child_sanic_app=Sanic("MyChildSanicApp")child_flask_app=Flask("MyChildFlaskApp")@app.middleware("request")asyncdefmodify_request(request):request.headers['Content-Type']="text/plain"@app.middleware("response")asyncdefmodify_response(request,response):response.body=response.body+b"\nModified by Sanic Response middleware!"response.headers['Content-Length']=len(response.body)returnresponse@app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_sanic_app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_flask_app.route("/")defindex():app=current_appreturnmake_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
传递给相关的register应用程序函数。默认情况下,对于所有注册的调度程序应用程序,apply_middleware
设置为False
。
在本例中,sanic请求中间件modify_request
将应用于所有请求,包括由在分派器上注册的应用程序处理的请求。在将请求中间件传递给任何已注册的应用程序之前,它将应用于request
。
在本例中,sanic响应中间件modify_response
将应用于所有响应,包括由在调度程序上注册的应用程序生成的响应。响应中间件将在注册应用程序处理后应用于response
。