如何通过WSGI实现Flask应用程序按路径分派? [英] How to implement Flask Application Dispatching by Path with WSGI?

查看:142
本文介绍了如何通过WSGI实现Flask应用程序按路径分派?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将单个域用作多个容器应用程序的分段环境,最终将运行在各自的域中。



类似于:


$ b

  • example_staging.com/app1

  • example_staging.com/app2

  • example_staging.com/app3



其中:


  • example_staging.com/app1的行为与app1.example_staging.com相同

  • example_staging.com/app2的行为与app2.example_staging.com相同

  • example_staging.com/app3的行为与app3.example_staging.com相同



或:


  • example_staging.com/app1的行为与app1.com相同

  • example_staging.com/app2的行为与app2.com相同

  • example_staging.com/app3的行为与app3.com相同



初级应用程序:

  from flask import Flask 

app = Flask(__ name__)

@ app.route('/')
def hello_wor ld():
返回'来自Flask的Hello!'

WSGI Starter Config File: / p>

  import sys 

project_home = u'/ home / path / sample1'

如果project_home不在sys.path中:
sys.path = [project_home] + sys.path

从应用程序导入应用程序作为应用程序



参考:

http://flask.pocoo.org/docs/0.10/patterns/appdispatch/



我不知道在哪里添加文档中给出的代码作为示例,以及create_app,default_app,get_user_for_prefix应该是什么样子。

注意:使用PythonAnywhere

解决方案

Glenns输入后的WSGI配置文件:

  import sys 

#将项目目录添加到sys.path
project_home = u'/ home / path / app1'
如果project_home不在sys.path中:
sys.path = [project_home] + sys.path

from werkzeug.wsgi import DispatcherMiddleware
从应用程序导入应用程序作为app1 $ b $从app2.app导入应用程序as app2
from app3.app import app as app3

application = DispatcherMiddleware(app1,{
'/ app2':app2,
'/ app3':app3
$)

文件夹结构:

  app1文件夹
app2文件夹
app3文件夹




创建每个单独的应用程序,并让他们在各自的域上进行响应。然后创建一个新的应用程序,从各个应用程序中导入应用程序变量,并使用 DispatcherMiddleware 您链接到文档页面上的标题为合并应用程序。


I would like to use a single domain as a Staging Environment for multiple flask applications that will eventually run on their own domains.

Something like:

  • example_staging.com/app1
  • example_staging.com/app2
  • example_staging.com/app3

where:

  • example_staging.com/app1 acts same as app1.example_staging.com
  • example_staging.com/app2 acts same as app2.example_staging.com
  • example_staging.com/app3 acts same as app3.example_staging.com

or:

  • example_staging.com/app1 acts same as app1.com
  • example_staging.com/app2 acts same as app2.com
  • example_staging.com/app3 acts same as app3.com

Starter app:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

WSGI Starter Config File:

import sys

project_home = u'/home/path/sample1'

if project_home not in sys.path:
    sys.path = [project_home] + sys.path

from app import app as application

Refering to:

http://flask.pocoo.org/docs/0.10/patterns/appdispatch/

I don't know where to add the code given in the documents as an example and what create_app, default_app, get_user_for_prefix should look like.

Note: Using PythonAnywhere

SOLUTION

WSGI Config File after Glenns input:

import sys

# add your project directory to the sys.path
project_home = u'/home/path/app1'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

from werkzeug.wsgi import DispatcherMiddleware
from app import app as app1
from app2.app import app as app2
from app3.app import app as app3

application = DispatcherMiddleware(app1, {
    '/app2':    app2,
    '/app3':    app3
})

Folder Structure:

app1 folder
    app2 folder
    app3 folder

解决方案

The key thing to note, here, is that you'll actually have 4 apps (3 individual apps and one combined app). This is ignoring the staging/live distinction because staging and live are just copies of each other in different directories.

Create each of the individual apps and get them responding on their individual domains. Then create a new app that imports the application variables from the individual apps and combines them using the DispatcherMiddleware like the example under the heading "Combining Applications" on the doc page you link to.

这篇关于如何通过WSGI实现Flask应用程序按路径分派?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆