gpt4 book ai didi

python - 如何在 Flask 中为共享应用程序后端的多个域提供服务?

转载 作者:行者123 更新时间:2023-12-03 15:46:11 26 4
gpt4 key购买 nike

我有两个 Web 域,分别称为 alpha.com 和 beta.com。我想在这两个不同的域上运行两个不同的面向公众的网站,但是来自共享服务器并在同一个 Flask 应用程序和应用程序上下文中。这些网站将共享后端应用程序和数据库,

我目前的项目结构如下:

app/
-static/
--alphacom_static/
--betacom_static/
--shared_backend_static/
-blueprints/
--alphacom_frontend_bluprint/
--betacom_frontend_blueprint/
--shared_backend_blueprint/

我正在通过服务器上的反向代理为在 localhost 上运行的 beta.com 提供flask/gevent。我打算只为 alpha.com 添加一个蓝图。

beta.com登陆页面的路径是 @blueprint.route(r'/', methods=['GET', 'POST']) .当用户在 beta.com/login 登录时,他们会被定向到 beta.com/app。

使用蓝图和路由的方法是什么,将 alpha.com 作为蓝图,并在用户登录时为 alpha.com/app 提供服务?

如何修改 alpha.com 的路由以避免与 beta.com 发生冲突?

最佳答案

我发现它在 Flask 当前的稳定版本 Flask==0.12.2 中没有得到很好的支持。 .理论上可以通过 host_matching 达到一定程度.但是在我的测试中,静态路由总是被破坏。

然而,在撰写本文时,master 上的 flask 开发版本已经合并了一个拉取请求,这使它变得更容易一些。做一个 pip install git+git://github.com/pallets/flask.git将安装 Flask==0.13.dev0 .然后,使用工厂模式创建flask app,你可以设置host_matching=Truestatic_host=127.0.0.1:8000就我而言。

对我来说,我的工厂函数如下所示:

def create_app(config_obj=None):
"""An application factory, as explained here:
http://flask.pocoo.org/docs/patterns/appfactories/.
:param config_object: The configuration object to use.
"""
app = Flask(__name__, host_matching=True, static_host='127.0.0.1:8000')
app.config.from_object(config_obj)
register_extensions(app)
register_blueprints(app)
return app

使这项工作所需的另一件事是修改您的主机并设置您要在主机文件中引用的域。在 Windows 上,可以在 C:\Windows\System32\drivers\etc\hosts 找到它。 .在 hosts 文件的底部,我已修改如下:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 alpha.local
127.0.0.1 beta.local
127.0.0.1 local

您需要在反向代理(Linux 上的 NGINX 或 Windows 上的 IIS)后面运行此解决方案,并将其设置为将适当的请求转发到 alpha.local:8000beta.local:8000在这个例子中。但是,您将修改 <subdomain>.local:<port>根据您的实际需要。

您将处理的另一个问题是浏览器提示 CORS 请求,因此您可能需要为 Access-Control-Allow-Origin: * 设置 header 。或特定域,如 Access-Control-Allow-Origin: http://beta.local:8000 .对于开发服务器,我发现这有助于 CORS 允许字体访问:
@blueprint.after_app_request
def add_headers_to_fontawesome_static_files(response):
"""
Fix for font-awesome files: after Flask static send_file() does its
thing, but before the response is sent, add an
Access-Control-Allow-Origin: *
HTTP header to the response (otherwise browsers complain).
"""
if not os.environ.get('APP_ENV') == 'prod':
if request.path and re.search(r'\.(ttf|woff|woff2|svg|eot)$', request.path):
response.headers.add('Access-Control-Allow-Origin', '*')
return response

请注意,对于生产,您必须在代理(如 NGINX 或 IIS)上设置修改后的 header ,并且上述功能对生产没有用。

最后,与 host_matching=True然后必须为主机指定路由,示例如下:
@blueprint.route('/about/', methods=['GET', 'POST'],
host='<string:subdom>.local:8000')
def about_app(**kwargs):
"""The about page."""
return render_template('about.html')

如果您按上述方式执行路由,则在应用程序某处的函数中设置 url_defualts 会很有帮助,如下所示:
@blueprint.app_url_defaults
def add_subdom(endpoint, values):
path = request.url_root
if 'alpha.local' in path:
g.subdom = 'alpha'
elif 'beta.local' in path:
g.subdom = 'beta'
if current_app.url_map.is_endpoint_expecting(endpoint, 'subdom'):
values['subdom'] = g.subdom

祝你好运,这并不容易。

关于python - 如何在 Flask 中为共享应用程序后端的多个域提供服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45767608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com