__init__.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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://sixth:skyeye@52.69.200.169: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_MAX_OVERFLOW'] = 15
  42. #設置session有效期為31天
  43. # app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=31)
  44. #設置上傳文件的路徑
  45. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  46. # 如果超過檔案大小限制,Flask將會拋出RequestEntityTooLarge的例外錯誤。
  47. app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB
  48. # 設定 JWT 密鑰
  49. app.config['JWT_SECRET_KEY'] = 'this-should-be-change'
  50. jwt.init_app(app)
  51. # app.config['JWT_TOKEN_LOCATION'] = ['headers', 'query_string']
  52. # app.config['JWT_TOKEN_LOCATION'] = ['query_string']
  53. # app.config['JWT_QUERY_STRING_NAME'] = 'jwt'
  54. app.config['PROPAGATE_EXCEPTIONS'] = True
  55. app.config['SECRET'] = 'my secret key'
  56. #MQTT相關設置
  57. app.config['MQTT_BROKER_URL'] = '60.250.156.234'
  58. app.config['MQTT_BROKER_PORT'] = 1883
  59. app.config['MQTT_USERNAME'] = 'aisky-client'
  60. app.config['MQTT_PASSWORD'] = 'aiskyc'
  61. #將發送ping到代理的時間間隔設置60秒
  62. app.config['MQTT_KEEPALIVE'] = 60
  63. #出於測試目的將TLS設置為禁用
  64. app.config['MQTT_TLS_ENABLED'] = False
  65. #數據庫的初始化
  66. db = SQLAlchemy(app)
  67. db.init_app(app)
  68. mqtt = Mqtt(app)
  69. socketio = SocketIO(app, cors_allowed_origins='*')
  70. #將main藍圖程序與app關聯到一起
  71. from .main import main as main_blueprint
  72. app.register_blueprint(main_blueprint)
  73. #將user藍圖程序與app關聯到一起
  74. from .user import user as user_blueprint
  75. app.register_blueprint(user_blueprint)
  76. return app, db, mqtt, socketio