__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #對整個應用做初始化操作
  2. #主要工作:
  3. # 1.構建Flask的應用以及各種配置
  4. #2.構建SQLAlchemy的應用
  5. from flask import Flask, g, request
  6. from flask_sqlalchemy import SQLAlchemy
  7. import pymysql
  8. import eventlet
  9. from flask_mqtt import Mqtt
  10. from flask_migrate import Migrate
  11. #from flask_migrate import Migrate, MigrateCommand
  12. from flask_babel import Babel
  13. # from werkzeug.wrappers import Request
  14. #綠化(綠色線程)所有系統模組,實現IO多路複用
  15. # eventlet.monkey_patch()
  16. pymysql.install_as_MySQLdb()
  17. # # db = SQLAlchemy()
  18. # def register_i18n(app):
  19. # """Register i18n with the Flask application."""
  20. # defalut_language_str = app.config['DEFAULT_LANGUAGE']
  21. # support_language_list = app.config['SUPPORTED_LANGUAGES']
  22. # # 1. 取得子資料夾路徑 land_code
  23. # @app.url_value_preprocessor
  24. # def get_lang_code(endpoint, values):
  25. # if values is not None:
  26. # g.lang_code = values.pop('lang_code', defalut_language_str)
  27. # # 2. 確認 config 內包含 lang_code
  28. # @app.before_request
  29. # def ensure_lang_support():
  30. # lang_code = g.get('lang_code', None)
  31. # if lang_code and lang_code not in support_language_list:
  32. # g.lang_code = request.accept_languages.best_match(support_language_list)
  33. # # 3. 設定多語系
  34. # @app.localeselector
  35. # def get_locale():
  36. # return g.get('lang_code')
  37. # # 4. 確認在 1. pop lang_code 參數後的 lang_code 值
  38. # @app.url_defaults
  39. # def set_langrage_code(endpoint, values):
  40. # if 'lang_code' in values or not g.lang_code:
  41. # return
  42. # if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
  43. # values['lang_code'] = g.lang_code
  44. def create_app():
  45. app = Flask(__name__)
  46. #配置啟動模式為調適模式
  47. # app.config['DEBUG'] = True
  48. #配置數據庫的連接字符串
  49. #app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://coffee:skyeye@52.69.200.169:3306/Coffee'
  50. # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:Gold@53743001@localhost:3306/Coffee'
  51. #app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:g53743001@localhost:3306/Coffee'
  52. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:g53743001@localhost:3306/coffee'
  53. #app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:Gold@53743001@192.168.50.106:3306/CoffeeManage'
  54. #配置數據庫內容再更新時自動提交
  55. app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
  56. #配置session所需要的秘鑰
  57. app.config['SECRET_KEY'] = 'you guess'
  58. # template 有修改後,會自動去更新
  59. app.config['TEMPLATES_AUTO_RELOAD'] = True
  60. #如果设置成True(默认情况),Flask - SQLAlchemy将会追踪对象的修改并且发送信号。这需要额外的内存, 如果不必要的可以禁用它
  61. #app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
  62. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
  63. #資料库连接池的大小。默认是数据库引擎的默认值 (通常是5)
  64. app.config['SQLALCHEMY_POOL_SIZE'] = 100
  65. #指定資料库连接池的超时时间。默认是 10
  66. # app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60
  67. #app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60 * 60
  68. app.config['SQLALCHEMY_POOL_TIMEOUT'] = 10
  69. #自动回收连接的秒数。这对 MySQL 是必须的,默认 情况下 MySQL 会自动移除闲置 8 小时或者以上的连接。
  70. #需要注意地是如果使用 MySQL 的话, Flask-SQLAlchemy 会自动地设置这个值为 2 小时
  71. #app.config['SQLALCHEMY_POOL_RECYCLE'] = 60 * 60 * 8
  72. app.config['SQLALCHEMY_POOL_RECYCLE'] = 1
  73. app.config['SQLALCHEMY_MAX_OVERFLOW'] = 15
  74. # app.config['SECRET'] = 'my secret key'
  75. #MQTT相關設置
  76. app.config['MQTT_BROKER_URL'] = '60.250.156.234'
  77. app.config['MQTT_BROKER_PORT'] = 1883
  78. app.config['MQTT_USERNAME'] = 'aisky-client'
  79. app.config['MQTT_PASSWORD'] = 'aiskyc'
  80. #將發送ping到代理的時間間隔設置60秒
  81. app.config['MQTT_KEEPALIVE'] = 60
  82. #出於測試目的將TLS設置為禁用
  83. app.config['MQTT_TLS_ENABLED'] = False
  84. #資料庫的初始化
  85. db = SQLAlchemy(app)
  86. db.init_app(app)
  87. mqtt = Mqtt(app)
  88. # 初始化Flask-Migrate擴展
  89. migrate = Migrate(app, db)
  90. app.config['BABEL_DEFAULT_LOCALE'] = 'zh'
  91. app.config['BABEL_DEFAULT_TIMEZONE'] = 'UTC'
  92. app.config['LANGUAGES'] = ['en', 'zh']
  93. babel = Babel(app)
  94. @babel.localeselector
  95. def get_locale():
  96. return request.accept_languages.best_match(app.config['LANGUAGES'])
  97. #將main藍圖程序與app關聯到一起
  98. from .main import main as main_blueprint
  99. app.register_blueprint(main_blueprint)
  100. #將user藍圖程序與app關聯到一起
  101. from .user import user as user_blueprint
  102. app.register_blueprint(user_blueprint)
  103. # Rita 將lang藍圖程序與app關聯到一起
  104. # from .main import main
  105. # app.register_blueprint(main, url_prefix='/<lang_code>')
  106. return app, db, mqtt