def create_app(config_name=config_map.get('develop')): # 创建 flask对象 app = Flask(__name__, template_folder='../template') # 设置flask的配置 app.config.from_object(config_name) # print(app.config) # db与app的初始化 # db.init_app(app=app) # 改用 init_model(app) 懒加载 # init_model(app=app) # 以后从exts中导入init_exts(app) 来初始化扩展 init_exts(app) # 初始化views 懒加载app的方式 init_view(app=app) apis_init_app(app=app) # # 注册蓝图, 这些功能放到views中的init 懒加载app # app.register_blueprint(index_bp) # 示例 # app.register_blueprint(house_bp, url_prefix='/houses') # 这里再写url_prefix 会覆盖蓝图文件中写的默认值, 注意要加/ # app.register_blueprint(blog_bp) # 正式 # app.register_blueprint(reg_bp) # app.register_blueprint(login_bp) return app
def create_app(config_name): """ 创建 app 对象 :return: """ app = Flask(__name__) config_class = config_map.get(config_name) app.config.from_object(config_class) global login_manager login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login' login_manager.refresh_view = 'login' # 响应保留 json 格式 app.config['JSON_AS_ASCII'] = False # 设置所有的视图函数允许跨域请求 CORS(app, supports_credentials=True) # CSRFProtect(app) global redis_store redis_store = redis.StrictRedis(host=config_class.REDIS_HOST, port=config_class.REDIS_PORT) # 利用 flask-session 将 session 数据保存到 redis 中 app.config['PERMANENT_SESSION_LIFETIME'] = 60 * 60 * 2 # 键有效期单位秒 session = Session() session.init_app(app) # 配置 qq 邮箱 app.config['MAIL_SERVER'] = 'smtp.qq.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_SSL'] = True app.config['MAIL_USE_TLS'] = False app.config['MAIL_USERNAME'] = '******' app.config['MAIL_PASSWORD'] = '******' # 放在此刻导入是为了解决循环导入 from App.api_v1 import api_v1_app app.register_blueprint(api_v1_app) global socket_io socket_io = SocketIO(cors_allowed_origins='*') socket_io.init_app(app) return app, socket_io
def create_app(config_name): """ 创建 apis 对象 :return: """ app = Flask(__name__, static_folder='../static') config_class = config_map.get(config_name) app.config.from_object(config_class) global login_manager login_manager = LoginManager() login_manager.init_app(app) app.config['JSON_AS_ASCII'] = False CORS(app, supports_credentials=True) # CSRFProtect(apis) global redis_store redis_store = redis.StrictRedis(host=config_class.HOST, port=config_class.REDIS_PORT) app.config['PERMANENT_SESSION_LIFETIME'] = 60 * 60 * 2 session = Session() session.init_app(app) # 配置 qq 邮箱 app.config['MAIL_SERVER'] = 'smtp.qq.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_SSL'] = True app.config['MAIL_USE_TLS'] = False app.config['MAIL_USERNAME'] = '******' app.config['MAIL_PASSWORD'] = '******' # 解决循环导入问题 from apis.common import api_common from apis.proxy import api_proxy from apis.wechat_mp import api_mp from apis.vsubscribe import api_vsubscribe app.register_blueprint(api_common) app.register_blueprint(api_mp) app.register_blueprint(api_proxy) app.register_blueprint(api_vsubscribe) return app
import redis import json from manager import MODEL from settings import config_map config_class = config_map.get(MODEL) class RedisModel(object): def __init__(self): if not hasattr(RedisModel, 'pool'): RedisModel.create_pool() self._connection = redis.Redis(connection_pool=RedisModel.pool) @staticmethod def create_pool(): RedisModel.pool = redis.ConnectionPool(host=config_class.REDIS_HOST, port=config_class.REDIS_PORT, password=config_class.REDIS_PWD, db=0) def set_data(self, key, value): """set data with (key, value) """ return self._connection.set(key, value) def get_data(self, key): """get data by key """
from flask import render_template from settings import config_map from apps import create_app app = create_app(config_map.get('develop')) # 这下面的默认 / 首页 放到apps/views/__init__.py中 # @app.route('/') # def show_index(): # return render_template('index.html') if __name__ == '__main__': print(app.url_map) app.run()