【问题标题】:How to deploy a Flask app with factory function for application creation如何部署具有工厂功能的 Flask 应用程序以创建应用程序
【发布时间】:2016-07-29 17:45:16
【问题描述】:

我已经成功地使用 .wsgi 文件中的以下配置部署了一个带有 wsgi 和 apache2 的单例烧瓶应用程序。

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/Devrupt/")

from Devrupt import app as application
application.secret_key = 'Add your secret key'

activate_this = '/var/www/Devrupt/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

我的 Apache2 .conf 文件:

<VirtualHost *:80>
        ServerName devrupt.com
        ServerAdmin lui@devrupt.com
        WSGIScriptAlias / /var/www/Devrupt/devrupt.wsgi
        <Directory /var/www/Devrupt/Devrupt/>
                Order allow,deny
                Allow from all
        </Directory>
        Alias /static /var/www/Devrupt/Devrupt/static
        <Directory /var/www/Devrupt/Devrupt/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

如何部署具有工厂功能的 Flask 应用程序来创建应用程序?

具有工厂函数结构的Flask Application:

| - Devrupt
    | - Devrupt
        |- app/
            |- templates/
            |- static/  
            |- main/
                |- __init__.py
                |- errors.py
                |- forms.py
                |- views.py
            |- __init__.py      # Factory Function
            |- models.py
        |- migrations/          # Contains the database migrations scripts
        |- tests/               # Where unit tests are written
            |- __init__.py
            |- test*.py
        |- venv/                # Contains the python virtual environment
        |- requirements.txt     # List package dependencies so that it is easy to regenerate an identical virtual environment on a diff machine
        |- config.py            # Stores the configuration settings
        |- manage.py            # Launch the application and other applcation tasks

| - devrupt.wsgi

mod_wsgi (Apache) 声明“如果您没有用于创建应用程序的工厂函数,但有一个单例实例,您可以直接将其作为应用程序导入。”

app/init.py(工厂函数):

# Application Package Constructor
from flask import Flask, render_template
from flask.ext.bootstrap import Bootstrap
from flask.ext.mail import Mail
from flask.ext.moment import Moment
from config import config

bootstrap = Bootstrap()
moment = Moment()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    moment.init_app(app)

    # Attach Routes and custom error messages here
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)


return app

我必须做哪些修改才能使其在工厂功能应用程序上工作?

【问题讨论】:

  • 为什么需要工厂?如果您成功地作为单身人士运行,那您就可以了。而且我们无法告诉您是否需要更改,因为工厂与目录结构无关,它是您在代码中定义的东西。你写过工厂函数吗?如果没有,那就是你需要开始的地方。

标签: python flask apache2 mod-wsgi wsgi


【解决方案1】:

您只需要确保您的.wsgi 文件中有一个应用程序对象。可以使用工厂函数创建此对象。例如:

from Devrupt import application_factory
application = application_factory()

就这么简单! 但要真正拼出来,请将 devrupt.wsgi 修改为:

#!/usr/bin/python
activate_this = 'var/www/Devrupt/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/")

from Devrupt import create_app
application = create_app('development')

【讨论】:

  • 所以我已将我的工厂函数(app/__init__.py)的代码添加到问题中。如何将应用程序对象添加到我的 .wsgi 文件中?
猜你喜欢
  • 2019-03-29
  • 2020-10-08
  • 1970-01-01
  • 2020-10-18
  • 2019-07-02
  • 2020-04-17
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
相关资源
最近更新 更多