__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #對整個應用做初始化操作
  2. #主要工作:
  3. # 1.構建Flask的應用以及各種配置
  4. #2.構建SQLAlchemy的應用
  5. from flask import Flask
  6. from flask_sqlalchemy import SQLAlchemy
  7. import pymysql
  8. import eventlet
  9. from flask_mqtt import Mqtt
  10. from datetime import timedelta
  11. from flask_jwt_extended import JWTManager
  12. from flask_socketio import SocketIO
  13. jwt = JWTManager()
  14. #綠化(綠色線程)所有系統模組,實現IO多路複用
  15. # eventlet.monkey_patch()
  16. pymysql.install_as_MySQLdb()
  17. #設置上傳文件路徑
  18. UPLOAD_FOLDER = 'app/static/farm_img'
  19. #設置允許文件副檔名
  20. ALLOWED_EXTENSIONS = set(['pdf', 'png', 'jpg', 'jpeg', 'gif'])
  21. def create_app():
  22. global UPLOAD_FOLDER
  23. app = Flask(__name__)
  24. #配置啟動模式為調適模式
  25. # app.config['DEBUG'] = True
  26. #配置數據庫的連接字符串
  27. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:Gold@53743001@localhost:3306/Sixth'
  28. #配置數據庫內容再更新時自動提交
  29. app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
  30. #配置session所需要的秘鑰
  31. app.config['SECRET_KEY'] = 'you guess'
  32. # template 有修改後,會自動去更新
  33. app.config['TEMPLATES_AUTO_RELOAD'] = True
  34. #如果设置成True(默认情况),Flask - SQLAlchemy将会追踪对象的修改并且发送信号。这需要额外的内存, 如果不必要的可以禁用它
  35. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
  36. #資料库连接池的大小。默认是数据库引擎的默认值 (通常是5)
  37. app.config['SQLALCHEMY_POOL_SIZE'] = 100
  38. #指定資料库连接池的超时时间。默认是 10
  39. app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60 * 60
  40. #自动回收连接的秒数。这对 MySQL 是必须的,默认 情况下 MySQL 会自动移除闲置 8 小时或者以上的连接。 需要注意地是如果使用 MySQL 的话, Flask-SQLAlchemy 会自动地设置这个值为 2 小时
  41. app.config['SQLALCHEMY_POOL_RECYCLE'] = 60 * 60 * 8
  42. app.config['SQLALCHEMY_MAX_OVERFLOW'] = 15
  43. #設置session有效期為31天
  44. # app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=31)
  45. #設置上傳文件的路徑
  46. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  47. # 如果超過檔案大小限制,Flask將會拋出RequestEntityTooLarge的例外錯誤。
  48. app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB
  49. # 設定 JWT 密鑰
  50. app.config['JWT_SECRET_KEY'] = 'this-should-be-change'
  51. jwt.init_app(app)
  52. # app.config['JWT_TOKEN_LOCATION'] = ['headers', 'query_string']
  53. # app.config['JWT_TOKEN_LOCATION'] = ['query_string']
  54. # app.config['JWT_QUERY_STRING_NAME'] = 'jwt'
  55. app.config['PROPAGATE_EXCEPTIONS'] = True
  56. app.config['SECRET'] = 'my secret key'
  57. #MQTT相關設置
  58. app.config['MQTT_BROKER_URL'] = '60.250.156.234'
  59. app.config['MQTT_BROKER_PORT'] = 1883
  60. app.config['MQTT_USERNAME'] = 'aisky-client'
  61. app.config['MQTT_PASSWORD'] = 'aiskyc'
  62. #將發送ping到代理的時間間隔設置60秒
  63. app.config['MQTT_KEEPALIVE'] = 60
  64. #出於測試目的將TLS設置為禁用
  65. app.config['MQTT_TLS_ENABLED'] = False
  66. #數據庫的初始化
  67. db = SQLAlchemy(app)
  68. db.init_app(app)
  69. mqtt = Mqtt(app)
  70. # socketio = SocketIO(app, cors_allowed_origins='*') # Rita 0428
  71. #將main藍圖程序與app關聯到一起
  72. from .main import main as main_blueprint
  73. app.register_blueprint(main_blueprint)
  74. #將user藍圖程序與app關聯到一起
  75. from .user import user as user_blueprint
  76. app.register_blueprint(user_blueprint)
  77. # return app, db, mqtt, socketio
  78. return app, db, mqtt