#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Contact : liuyuqi.gov@msn.cn @Time : 2023/11/09 14:27:17 @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved. @Desc : create flask app ''' import os from flask import Flask from apps.config import config from apps.views import init_blueprints from apps.extensions import init_plugins def create_app(config_name="default") -> Flask: ''' create app ''' config_name = os.getenv('FLASK_CONFIG') or config_name # CORS(app, supports_credentials=True, resources={ # r"/api/*": {'origins': "*"} # }) app = Flask(config[config_name].BASE_DIR) app.config.from_object(config[config_name]) # 读取配置 config[config_name].init_app(app) init_dir() init_blueprints(app) init_plugins(app) init_hook(app) return app def init_dir(): ''' init dir ''' files = [ 'tmp/ct', 'tmp/draw', 'tmp/image', 'tmp/mask', 'tmp/uploads' ] for ff in files: if not os.path.exists(ff): os.makedirs(ff) def init_hook(app: Flask): ''' init hook ''' @app.after_request def after_request(response): ''' resolve cross-domain ''' # response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # response.headers["Expires"] = 0 # response.headers["Pragma"] = "no-cache" # response.set_cookie('remember_token', '', expires=0) # response.headers.add('X-Version', app.config['CURRENT_VERSION']) # response.headers.add('X-Env', app.config['DEPLOY_ENV']) response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Credentials'] = 'true' response.headers['Access-Control-Allow-Methods'] = 'POST' response.headers['Access-Control-Allow-Headers'] = 'Content-Type, X-Requested-With' return response @app.before_request def before_request(): pass