#對整個應用做初始化操作 #主要工作: # 1.構建Flask的應用以及各種配置 #2.構建SQLAlchemy的應用 from flask import Flask, g, request from flask_sqlalchemy import SQLAlchemy import pymysql import eventlet from flask_mqtt import Mqtt from flask_migrate import Migrate #from flask_migrate import Migrate, MigrateCommand from flask_babel import Babel # from werkzeug.wrappers import Request #綠化(綠色線程)所有系統模組,實現IO多路複用 # eventlet.monkey_patch() pymysql.install_as_MySQLdb() # # db = SQLAlchemy() # def register_i18n(app): # """Register i18n with the Flask application.""" # defalut_language_str = app.config['DEFAULT_LANGUAGE'] # support_language_list = app.config['SUPPORTED_LANGUAGES'] # # 1. 取得子資料夾路徑 land_code # @app.url_value_preprocessor # def get_lang_code(endpoint, values): # if values is not None: # g.lang_code = values.pop('lang_code', defalut_language_str) # # 2. 確認 config 內包含 lang_code # @app.before_request # def ensure_lang_support(): # lang_code = g.get('lang_code', None) # if lang_code and lang_code not in support_language_list: # g.lang_code = request.accept_languages.best_match(support_language_list) # # 3. 設定多語系 # @app.localeselector # def get_locale(): # return g.get('lang_code') # # 4. 確認在 1. pop lang_code 參數後的 lang_code 值 # @app.url_defaults # def set_langrage_code(endpoint, values): # if 'lang_code' in values or not g.lang_code: # return # if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'): # values['lang_code'] = g.lang_code def create_app(): app = Flask(__name__) #配置啟動模式為調適模式 # app.config['DEBUG'] = True #配置數據庫的連接字符串 #app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://coffee:skyeye@52.69.200.169:3306/Coffee' # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:Gold@53743001@localhost:3306/Coffee' #app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:g53743001@localhost:3306/Coffee' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:g53743001@localhost:3306/coffee' #app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:Gold@53743001@192.168.50.106:3306/CoffeeManage' #配置數據庫內容再更新時自動提交 app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True #配置session所需要的秘鑰 app.config['SECRET_KEY'] = 'you guess' # template 有修改後,會自動去更新 app.config['TEMPLATES_AUTO_RELOAD'] = True #如果设置成True(默认情况),Flask - SQLAlchemy将会追踪对象的修改并且发送信号。这需要额外的内存, 如果不必要的可以禁用它 #app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False #資料库连接池的大小。默认是数据库引擎的默认值 (通常是5) app.config['SQLALCHEMY_POOL_SIZE'] = 100 #指定資料库连接池的超时时间。默认是 10 # app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60 #app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60 * 60 app.config['SQLALCHEMY_POOL_TIMEOUT'] = 10 #自动回收连接的秒数。这对 MySQL 是必须的,默认 情况下 MySQL 会自动移除闲置 8 小时或者以上的连接。 #需要注意地是如果使用 MySQL 的话, Flask-SQLAlchemy 会自动地设置这个值为 2 小时 #app.config['SQLALCHEMY_POOL_RECYCLE'] = 60 * 60 * 8 app.config['SQLALCHEMY_POOL_RECYCLE'] = 1 app.config['SQLALCHEMY_MAX_OVERFLOW'] = 15 # app.config['SECRET'] = 'my secret key' #MQTT相關設置 app.config['MQTT_BROKER_URL'] = '60.250.156.234' app.config['MQTT_BROKER_PORT'] = 1883 app.config['MQTT_USERNAME'] = 'aisky-client' app.config['MQTT_PASSWORD'] = 'aiskyc' #將發送ping到代理的時間間隔設置60秒 app.config['MQTT_KEEPALIVE'] = 60 #出於測試目的將TLS設置為禁用 app.config['MQTT_TLS_ENABLED'] = False #資料庫的初始化 db = SQLAlchemy(app) db.init_app(app) mqtt = Mqtt(app) # 初始化Flask-Migrate擴展 migrate = Migrate(app, db) app.config['BABEL_DEFAULT_LOCALE'] = 'zh' app.config['BABEL_DEFAULT_TIMEZONE'] = 'UTC' app.config['LANGUAGES'] = ['en', 'zh'] babel = Babel(app) @babel.localeselector def get_locale(): return request.accept_languages.best_match(app.config['LANGUAGES']) #將main藍圖程序與app關聯到一起 from .main import main as main_blueprint app.register_blueprint(main_blueprint) #將user藍圖程序與app關聯到一起 from .user import user as user_blueprint app.register_blueprint(user_blueprint) # Rita 將lang藍圖程序與app關聯到一起 # from .main import main # app.register_blueprint(main, url_prefix='/') return app, db, mqtt