config.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import os
  2. import random
  3. import string
  4. import dotenv
  5. import logging
  6. from datetime import timedelta
  7. from dotenv import load_dotenv
  8. if os.path.exists('.env'):
  9. load_dotenv('.env', verbose=True)
  10. class BaseConfig:
  11. ''' 基础配置 '''
  12. # UPLOAD_FOLDER = r'./uploads'
  13. # app = Flask(__name__)
  14. # app.secret_key = 'secret!'
  15. # app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  16. # werkzeug_logger = rel_log.getLogger('werkzeug')
  17. # werkzeug_logger.setLevel(rel_log.ERROR)
  18. # # 解决缓存刷新问题
  19. # app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1)
  20. SUPERADMIN = 'admin'
  21. ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
  22. STATIC_URL = '/static/'
  23. SECRET_KEY = os.getenv('SECRET_KEY', None)
  24. if not SECRET_KEY:
  25. SECRET_KEY = ''.join(random.choice(string.ascii_lowercase)
  26. for i in range(32))
  27. # base
  28. PROJECT_NAME = 'flask_blog'
  29. # 当前 config.py 文件目录
  30. DEBUG = False
  31. # 当前文件所在目录的绝对路径 /apps
  32. BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
  33. BASE_URL = 'http://'
  34. ADMIN_EMAILS = ""
  35. # 分页
  36. POST_PER_PAGE = 8 # 每页文章数
  37. COMMENT_PER_PAGE = 10 # 每页评论数
  38. LANGUAGE_CODE = 'zh-hans'
  39. TIME_ZONE = 'PRC'
  40. UP_DIR = os.path.join(os.path.abspath(
  41. os.path.dirname(__file__)), "app/static/uploads/") # 文件上传路径
  42. FC_DIR = os.path.join(os.path.abspath(os.path.dirname(
  43. __file__)), "app/static/uploads/users/") # 用户头像上传路径
  44. UPLOADED_PHOTOS_DEST = 'static/upload'
  45. UPLOADED_FILES_ALLOW = ['gif', 'jpg']
  46. UPLOADS_AUTOSERVE = True
  47. JSON_AS_ASCII = False
  48. # db
  49. SQLALCHEMY_TRACK_MODIFICATIONS = False
  50. DB_ENGINE = os.getenv('DB_ENGINE', None)
  51. DB_USERNAME = os.getenv('DB_USERNAME', None)
  52. DB_PASS = os.getenv('DB_PASS', None)
  53. DB_HOST = os.getenv('DB_HOST', None)
  54. DB_PORT = os.getenv('DB_PORT', None)
  55. DB_NAME = os.getenv('DB_NAME', None)
  56. USE_SQLITE = True
  57. # redis
  58. REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
  59. REDIS_PORT = os.getenv('REDIS_PORT', 6379)
  60. REDIS_DB = os.getenv('REDIS_DB', 0)
  61. REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', None)
  62. CELERY_BROKER_URL = 'redis://{}:{}/{}'.format(
  63. REDIS_HOST, REDIS_PORT, REDIS_DB)
  64. # mail
  65. MAIL_SERVER = os.getenv('MAIL_SERVER', 'smtp.qq.com')
  66. MAIL_PORT = os.getenv('MAIL_PORT', 465)
  67. MAIL_USE_TLS = False
  68. MAIL_USE_SSL = True
  69. MAIL_DEBUG = True
  70. MAIL_USERNAME = os.getenv('MAIL_USERNAME', None)
  71. MAIL_PASSWORD = os.getenv('MAIL_PASSWORD', None) # 生成的授权码
  72. MAIL_DEFAULT_SENDER = MAIL_USERNAME
  73. # token配置
  74. TIMEOUT_TOKEN = 10 * 24 * 60 * 60
  75. TIMEOUT_EMAIL = 10 * 60
  76. PERMANENT_SESSION_LIFETIME = timedelta(hours=2)
  77. # log
  78. LOG_LEVEL = logging.WARN
  79. # 插件配置,填写插件的文件名名称,默认不启用插件。
  80. PLUGIN_ENABLE_FOLDERS = []
  81. # 七牛云
  82. QINIU_ACCESS_KEY = os.getenv('QINIU_ACCESS_KEY', None)
  83. QINIU_SECRET_KEY = os.getenv('QINIU_SECRET_KEY', None)
  84. QINIU_BUCKET_NAME = os.getenv('QINIU_BUCKET_NAME', None)
  85. # github
  86. GITHUB_CLIENT_ID = os.getenv('GITHUB_CLIENT_ID', None)
  87. GITHUB_CLIENT_SECRET = os.getenv('GITHUB_CLIENT_SECRET', None)
  88. if GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET:
  89. SOCIAL_AUTH_GITHUB = True
  90. @staticmethod
  91. def init_app(app):
  92. pass
  93. class DevelopmentConfig(BaseConfig):
  94. ''' 开发环境配置, 开启调试模式, 使用 sqlite '''
  95. DEBUG = True
  96. SQLALCHEMY_TRACK_MODIFICATIONS = True
  97. # SQLALCHEMY_ECHO = True
  98. # SQLALCHEMY_COMMIT_ON_TEARDOWN = True
  99. SQLALCHEMY_DATABASE_URI = 'sqlite:///../flask-note.sqlite3.db'
  100. class TestingConfig(BaseConfig):
  101. DEBUG = True
  102. class ProductionConfig(BaseConfig):
  103. DEBUG = False
  104. # SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite3'
  105. SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://lyq:123456@ok.yoqi.me:13306/travel?charset=utf8'
  106. # Security
  107. SESSION_COOKIE_HTTPONLY = True
  108. REMEMBER_COOKIE_HTTPONLY = True
  109. REMEMBER_COOKIE_DURATION = 3600
  110. config = {
  111. 'default': DevelopmentConfig,
  112. 'development': DevelopmentConfig,
  113. 'production': ProductionConfig
  114. }