Пример #1
0
def create_app(config_var=os.getenv('DEPLOY_ENV', 'Development')):
    # init application
    app = Flask(__name__)
    app.config.from_object('pxa.config.%sConfig' % config_var)

    # configure logger
    configure_logger(app)

    # configure celery
    celery.config_from_object(app.config)

    # configure elasticsearch
    es.init_app(app)

    # init database
    db.init_app(app)
    _module_dir = os.path.dirname(os.path.abspath(__file__))
    migrate.init_app(app, db, directory=os.path.join(_module_dir, '..', 'migrations'))  # noqa

    app.signer = TimestampSigner(app.config['SIGNER_KEY'])

    # register Blueprints
    from pxa.views.common import bp_common
    app.register_blueprint(bp_common)
    from pxa.views.website import bp_website
    app.register_blueprint(bp_website)

    # install error handler for views
    error_codes = [400, 401, 403, 404, 405, 406, 408, 409, 410, 412, 415, 428,
                   429, 500, 501]

    install_error_handlers(error_codes, app)
    return app
Пример #2
0
def get_wsgi_app():

    # 定义了静态目录资源, 以及静态资源的访问context
    app = Flask(__name__, static_url_path='', static_folder='static')

    # Werkzeug 的 ProxyFix 来修复代理请求的问题
    app.wsgi_app = ProxyFix(app.wsgi_app)

    # config
    app.config.from_object(Config)

    blueprints = (
        # 用户侧
        bpm_member,
        bpm_blog,
        bpm_comment,
    )

    for bp in blueprints:
        app.register_blueprint(bp)

    # DB
    app.sa_engine = engine_from_config(app.config["DB_OPTIONS"], prefix="")
    app.DBSession = scoped_session(sessionmaker(bind=app.sa_engine))

    app.signer = URLSafeSerializer(app.config["SECRET_KEY"])

    # 解决print或者文件读入写出时UnicodeEncodeError问题
    reload(sys)
    sys.setdefaultencoding('utf-8')

    @app.before_request
    def _before_request():

        #将传入的json信息转为dict并转为全局变量
        g.params = request.get_json()

        if request.endpoint == "static":
            return
        g.db = current_app.DBSession()
        if current_app.config.get('SESSION_ROLLBACK'):
            g.db.begin_nested()

    # @app.after_request
    # def _after_request(response):
    #     return response

    # @app.teardown_request
    # def _teardown_request(exception):
    #     if request.endpoint == "static":
    #         return
    #     if current_app.config.get('SESSION_ROLLBACK'):
    #         g.db.rollback()
    #     current_app.DBSession.remove()

    return app
Пример #3
0
def create_app(environment=os.getenv('ENVIRONMENT', 'Development')):
    app = Flask(__name__)
    app.config.from_object(f'complainio.config.{environment}')
    logger.setLevel(app.config['LOGS_LEVEL'])

    app.json_encoder = JSONEncoder
    app.signer = TimestampSigner(app.config['SECRET_KEY'])

    if not environment == 'Testing':
        mongo.init_app(app)

    from complainio.views.api_v1 import api_v1
    app.register_blueprint(api_v1)

    return app
from flask import Flask
from config import config
from flask.ext.cors import CORS
import itsdangerous

flask_app = Flask(__name__)
flask_app.debug = config.debug()
flask_app.config.update(
    SQLALCHEMY_DATABASE_URI=config.database_connection_string(),
    CORS_HEADERS="Content-Type",
    CORS_RESOURCES={
        r"/api/*": {
            "origins": config.allowed_origins()
        }
    })

CORS(flask_app, supports_credentials=True)

flask_app.secret_key = config.secret_key()
flask_app.signer = itsdangerous.URLSafeTimedSerializer(flask_app.secret_key)
Пример #5
0
import os
from flask import Flask
from itsdangerous import TimestampSigner

app = Flask(__name__)

def get_env():
    local_settings_found = os.path.exists(os.path.join(os.path.dirname(__file__), 'config', 'local.py'))
    return os.environ.get('SPARK_APPLICATION_SETTINGS', 'local'  if local_settings_found else 'base')

def get_conf():
    return 'spark.config.%s.Configuration' % get_env()

app.config.from_object(get_conf())
app.signer = TimestampSigner(app.config['SIGNER_KEY'])

from . import views
from . import api


Пример #6
0
            returns = app.signer.issue_signature(request.data)
            return dumps({"status": "OK", "returns": returns})
        except Exception as e:
            print(e)
            return dumps({"status": "ERROR", "message": e.args})
    else:
        return dumps({"status": "ERROR", "message": "Use POST method."})


# send verification key
@app.route("/key", methods=["GET", "POST"])
def app_key():
    if request.method == "POST":
        try:
            returns = app.signer.send_vk()
            return dumps({"status": "OK", "returns": returns})
        except Exception as e:
            print(e)
            return dumps({"status": "ERROR", "message": e.args})
    else:
        return dumps({"status": "ERROR", "message": "Use POST method."})


# ==================================================
# entry point
# ==================================================
if __name__ == '__main__':
    port = sys.argv[1]
    app.signer = Signer()
    app.run(host="127.0.0.1", port=port)
Пример #7
0
def _configure_extensions(app: Flask):
    logging.config.dictConfig(app.config['LOGGING_CONFIG'])
    app.signer = TimestampSigner(app.config['SIGNER_KEY'])