config.py

  1. import io
  2. import logging
  3. import os
  4. from contextlib import contextmanager
  5. from functools import lru_cache
  6. from io import StringIO
  7. from typing import Optional
  8. from dotenv.main import DotEnv
  9. from pydantic import BaseSettings, Field
  10. logger = logging.getLogger(__name__)
  11. def my_get_stream(self):
  12. """重写python-dotenv读取文件的方法,使用utf-8,支持读取包含中文的.env配置文件"""
  13. if isinstance(self.dotenv_path, StringIO):
  14. yield self.dotenv_path
  15. elif os.path.isfile(self.dotenv_path):
  16. with io.open(self.dotenv_path, encoding='utf-8') as stream:
  17. yield stream
  18. else:
  19. if self.verbose:
  20. logger.warning("File doesn't exist %s", self.dotenv_path)
  21. yield StringIO('')
  22. DotEnv._get_stream = contextmanager(my_get_stream)
  23. class Settings(BaseSettings):
  24. """System configurations."""
  25. # 系统环境
  26. ENVIRONMENT: Optional[str] = Field(None, env="ENVIRONMENT")
  27. # 系统安全秘钥
  28. SECRET_KEY = 'ZEuk2U9svM2WRJql4Fs2lEvD05ZDQXZdKboim__SQqsUUqJwStZJq6u0e30bIL4Qe80PB48X1dcIZHjxqLzUiA'
  29. # API版本号
  30. API_V1_STR = "/api/v1"
  31. # token过期时间
  32. ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 8
  33. # 算法
  34. ALGORITHM = "HS256"
  35. # 产品名称
  36. CDN_PRODUCTION_NAME = {
  37. "cdn": "cdn"
  38. }
  39. # 加载.env文件的配置
  40. class Config:
  41. env_file = ".env"
  42. case_sensitive = True
  43. class DevConfig(Settings):
  44. """Development configurations."""
  45. # redis
  46. REDIS_HOST: Optional[str] = Field(None, env="DEV_REDIS_HOST")
  47. REDIS_PORT: Optional[int] = Field(None, env="DEV_REDIS_PORT")
  48. REDIS_USERNAME: Optional[str] = Field(None, env="DEV_REDIS_USERNAME")
  49. REDIS_PASSWORD: Optional[str] = Field(None, env="DEV_REDIS_PASSWORD")
  50. REDIS_DB: Optional[int] = Field(None, env="DEV_REDIS_DB")
  51. # 七牛玉
  52. QINIU_ACCESS_KEY: Optional[str] = Field(None, env="DEV_QINIU_ACCESS_KEY")
  53. QINIU_SECRET_KEY: Optional[str] = Field(None, env="DEV_QINIU_SECRET_KEY")
  54. QINIU_BUCKET_NAME: Optional[str] = Field(None, env="DEV_QINIU_BUCKET_NAME")
  55. QINIU_DOMAIN_NAME: Optional[str] = Field(None, env="DEV_QINIU_DOMAIN_NAME")
  56. # Tencent
  57. TENCENT_SECRET_KEY: Optional[str] = Field(None, env="DEV_TENCENT_SECRET_KEY")
  58. TENCENT_ACCESS_ID: Optional[str] = Field(None, env="DEV_TENCENT_ACCESS_ID")
  59. # Mysql
  60. MYSQL_SERVER: Optional[str] = Field(None, env="DEV_MYSQL_SERVER")
  61. MYSQL_USER: Optional[str] = Field(None, env="DEV_MYSQL_USER")
  62. MYSQL_PASSWORD: Optional[str] = Field(None, env="DEV_MYSQL_PASSWORD")
  63. MYSQL_DB_NAME: Optional[str] = Field(None, env="DEV_MYSQL_DB_NAME")
  64. MYSQL_PORT: Optional[int] = Field(None, env="DEV_MYSQL_PORT")
  65. class TestConfig(Settings):
  66. """Production configurations."""
  67. REDIS_HOST: Optional[str] = Field(None, env="TEST_REDIS_HOST")
  68. REDIS_PORT: Optional[int] = Field(None, env="TEST_REDIS_PORT")
  69. REDIS_USERNAME: Optional[str] = Field(None, env="TEST_REDIS_USERNAME")
  70. REDIS_PASSWORD: Optional[str] = Field(None, env="TEST_REDIS_PASSWORD")
  71. REDIS_DB: Optional[int] = Field(None, env="TEST_REDIS_DB")
  72. QINIU_ACCESS_KEY: Optional[str] = Field(None, env="TEST_QINIU_ACCESS_KEY")
  73. QINIU_SECRET_KEY: Optional[str] = Field(None, env="TEST_QINIU_SECRET_KEY")
  74. QINIU_BUCKET_NAME: Optional[str] = Field(None, env="TEST_QINIU_BUCKET_NAME")
  75. QINIU_DOMAIN_NAME: Optional[str] = Field(None, env="TEST_QINIU_DOMAIN_NAME")
  76. TENCENT_SECRET_KEY: Optional[str] = Field(None, env="TEST_TENCENT_SECRET_KEY")
  77. TENCENT_ACCESS_ID: Optional[str] = Field(None, env="TEST_TENCENT_ACCESS_ID")
  78. MYSQL_SERVER: Optional[str] = Field(None, env="TEST_MYSQL_SERVER")
  79. MYSQL_USER: Optional[str] = Field(None, env="TEST_MYSQL_USER")
  80. MYSQL_PASSWORD: Optional[str] = Field(None, env="TEST_MYSQL_PASSWORD")
  81. MYSQL_DB_NAME: Optional[str] = Field(None, env="TEST_MYSQL_DB_NAME")
  82. MYSQL_PORT: Optional[int] = Field(None, env="TEST_MYSQL_PORT")
  83. class ProdConfig(Settings):
  84. """Production configurations."""
  85. REDIS_HOST: Optional[str] = Field(None, env="PROD_REDIS_HOST")
  86. REDIS_PORT: Optional[int] = Field(None, env="PROD_REDIS_PORT")
  87. REDIS_USERNAME: Optional[str] = Field(None, env="PROD_REDIS_USERNAME")
  88. REDIS_PASSWORD: Optional[str] = Field(None, env="PROD_REDIS_PASSWORD")
  89. REDIS_DB: Optional[int] = Field(None, env="PROD_REDIS_DB")
  90. QINIU_ACCESS_KEY: Optional[str] = Field(None, env="PROD_QINIU_ACCESS_KEY")
  91. QINIU_SECRET_KEY: Optional[str] = Field(None, env="PROD_QINIU_SECRET_KEY")
  92. QINIU_BUCKET_NAME: Optional[str] = Field(None, env="PROD_QINIU_BUCKET_NAME")
  93. QINIU_DOMAIN_NAME: Optional[str] = Field(None, env="PROD_QINIU_DOMAIN_NAME")
  94. TENCENT_SECRET_KEY: Optional[str] = Field(None, env="PROD_TENCENT_SECRET_KEY")
  95. TENCENT_ACCESS_ID: Optional[str] = Field(None, env="PROD_TENCENT_ACCESS_ID")
  96. MYSQL_SERVER: Optional[str] = Field(None, env="PROD_MYSQL_SERVER")
  97. MYSQL_USER: Optional[str] = Field(None, env="PROD_MYSQL_USER")
  98. MYSQL_PASSWORD: Optional[str] = Field(None, env="PROD_MYSQL_PASSWORD")
  99. MYSQL_DB_NAME: Optional[str] = Field(None, env="PROD_MYSQL_DB_NAME")
  100. MYSQL_PORT: Optional[int] = Field(None, env="PROD_MYSQL_PORT")
  101. class FactoryConfig:
  102. """Returns a config instance dependending on the ENV_STATE variable."""
  103. def __init__(self, env_state: Optional[str]):
  104. self.env_state = env_state
  105. def __call__(self):
  106. if self.env_state == "development":
  107. return DevConfig()
  108. elif self.env_state == "production":
  109. return ProdConfig()
  110. elif self.env_state == "testing":
  111. return TestConfig()
  112. @lru_cache()
  113. def get_configs():
  114. """加载一下环境文件"""
  115. from dotenv import load_dotenv
  116. load_dotenv(encoding='utf-8')
  117. return FactoryConfig(Settings().ENVIRONMENT)()
  118. configs = get_configs()

.env

  1. # 服务器环境,指明生产服务器,开发服务器,测试服务器来动态触发配置变量的调用
  2. # 系统中设置的环境变量优先于此处设置的环境变量
  3. ENVIRONMENT = "development" # production or development or testing
  4. # 开发服务器环境的配置
  5. # redis配置
  6. DEV_REDIS_HOST=
  7. DEV_REDIS_PORT="6379"
  8. DEV_REDIS_USERNAME="root"
  9. DEV_REDIS_PASSWORD="123456"
  10. DEV_REDIS_DB="1"
  11. # 七牛云配置
  12. DEV_QINIU_ACCESS_KEY =
  13. DEV_QINIU_SECRET_KEY =
  14. DEV_QINIU_BUCKET_NAME = 'dengsz'
  15. DEV_QINIU_DOMAIN_NAME = 'http://qayumwil2.bkt.clouddn.com/'
  16. # 腾讯CND配置
  17. DEV_TENCENT_SECRET_KEY = '中文'
  18. DEV_TENCENT_ACCESS_ID = 'tencent4'
  19. # mysql数据库配置
  20. DEV_MYSQL_SERVER =
  21. DEV_MYSQL_USER = 'root'
  22. DEV_MYSQL_PASSWORD = '123456'
  23. DEV_MYSQL_DB_NAME = 'users'
  24. DEV_MYSQL_PORT = '3306'
  25. # 生产服务器环境配置
  26. PROD_REDIS_HOST=
  27. PROD_REDIS_PORT="6379"
  28. PROD_REDIS_USERNAME="root"
  29. PROD_REDIS_PASSWORD="123456"
  30. PROD_REDIS_DB="1"
  31. PROD_QINIU_ACCESS_KEY =
  32. PROD_QINIU_SECRET_KEY =
  33. PROD_QINIU_BUCKET_NAME = 'dengsz'
  34. PROD_QINIU_DOMAIN_NAME = 'http://qayumwil2.bkt.clouddn.com/'
  35. PROD_TENCENT_SECRET_KEY = 'tencent3'
  36. PROD_TENCENT_ACCESS_ID = 'tencent4'
  37. PROD_MYSQL_SERVER =
  38. PROD_MYSQL_USER = 'root'
  39. PROD_MYSQL_PASSWORD = 'cdndev'
  40. PROD_MYSQL_DB_NAME = 'cdn_users'
  41. PROD_MYSQL_PORT = '3306'
  42. # 测试服务器环境配置
  43. TEST_REDIS_HOST=
  44. TEST_REDIS_PORT="6379"
  45. TEST_REDIS_USERNAME="root"
  46. TEST_REDIS_PASSWORD="123456"
  47. TEST_REDIS_DB="1"
  48. TEST_QINIU_ACCESS_KEY =
  49. TEST_QINIU_SECRET_KEY =
  50. TEST_QINIU_BUCKET_NAME = 'dengsz'
  51. TEST_QINIU_DOMAIN_NAME = 'http://qayumwil2.bkt.clouddn.com/'
  52. TEST_TENCENT_SECRET_KEY = 'tencent3'
  53. TEST_TENCENT_ACCESS_ID = 'tencent4'
  54. TEST_MYSQL_SERVER = '127.0.0.1'
  55. TEST_MYSQL_USER = 'root'
  56. TEST_MYSQL_PASSWORD = '123456'
  57. TEST_MYSQL_DB_NAME = 'users'
  58. TEST_MYSQL_PORT = '3306'