from __future__ import annotations import json from typing import Optional, Union from pydantic import BaseSettings, Field class GlobalConfig(BaseSettings): """Global configurations.""" # This variable will be loaded from the .env file. However, if there is a # shell environment variable having the same name, that will take precedence. # the class Field is necessary while defining the global variables ENV_STATE: Optional[str] = Field('dev', env="ENV_STATE") KAHKA_HOST: Optional[str] = Field('127.0.0.1', env="CK_HOST") # environment specific configs # CK_USERNAME: Optional[str] = None # API_PASSWORD: Optional[str] = None PROJECT_NAME: str = 'CHECK_DATA' API_V1_STR: str = '/v1' class Config: """Loads the dotenv file.""" env_file: str = ".env" DEFAULT_FIELD: dict = { '#ip': 'ipv4', '#country': 'string', '#province': 'string', '#city': 'string', '#os': 'string', '#device_id': 'string', '#screen_height': 'integer', '#screen_width': 'integer', '#device_model': 'string', '#app_version': 'string', '#bundle_id': 'string', 'app_name': 'string', 'game_version': 'string', '#os_version': 'string', '#network_type': 'string', '#carrier': 'string', '#manufacturer': 'string', '#app_id': 'string', '#account_id': 'string', '#distinct_id': 'string', 'binduid': 'string', 'channel': 'string', 'owner_name': 'string', 'role_name': 'string', 'exp': 'integer', 'zhanli': 'integer', 'maxmapid': 'string', 'mapid': 'string', 'ghid': 'string', 'rmbmoney': 'integer', 'jinbi': 'integer', 'svrindex': 'string', 'lv': 'integer', 'vip': 'integer', 'game': 'string', # 'unitPrice': 'integer', # 'money': 'string', # 'isdangrishouci': 'integer', # 'islishishouci': 'integer', # 'is_today_reg': 'integer', # 'orderid': 'string', # 'proid': 'string', # # 'step_id': 'integer', # 'step_group': 'integer', # 'guide_start_time': 'integer', # # 'online_ts': 'integer' } class DevConfig(GlobalConfig): """Development configurations.""" # ck数据库连接 CK_CONFIG = {'host': '139.159.159.3', 'port': 9654, 'user': 'legu', 'password': 'gncPASUwpYrc' } class Config: env_prefix: str = "DEV_" MDB_HOST: str = '10.0.0.7' MDB_PORT: int = 27017 MDB_USER: str = 'root' MDB_PASSWORD: str = 'iamciniao' MDB_DB: str = 'xdata' DATABASE_URI = f'mongodb://{MDB_USER}:{MDB_PASSWORD}@{MDB_HOST}:{MDB_PORT}/admin' class ProdConfig(GlobalConfig): """Production configurations.""" # ck数据库连接 CK_CONFIG = {'host': '139.159.159.3', 'port': 9654, 'user': 'legu', 'password': 'gncPASUwpYrc' } class Config: env_prefix: str = "PROD_" class FactoryConfig: """Returns a config instance dependending on the ENV_STATE variable.""" def __init__(self, env_state: Optional[str]) -> None: self.env_state = env_state def __call__(self) -> Union[DevConfig, ProdConfig]: if self.env_state == "dev": return DevConfig() elif self.env_state == "prod": return ProdConfig() settings = FactoryConfig(GlobalConfig().ENV_STATE)() # print(config.KAHKA_HOST) # print(config.__repr__())