|
| 1 | +from functools import lru_cache |
| 2 | +from typing import List, Optional, Union |
| 3 | + |
| 4 | +from decouple import config |
| 5 | +from pydantic import AnyHttpUrl, BaseSettings, HttpUrl, validator |
| 6 | +from pydantic.networks import AnyUrl |
| 7 | + |
| 8 | + |
| 9 | +class Credentials(BaseSettings): |
| 10 | + AWS_ACCESS_KEY_ID = config('AWS_ID') |
| 11 | + AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_KEY') |
| 12 | + |
| 13 | + DATABASE_URL: AnyUrl = config('DATABASE_URL') |
| 14 | + |
| 15 | + |
| 16 | +class Settings(Credentials): |
| 17 | + API_V1_STR: str = '/api/v1' |
| 18 | + |
| 19 | + ENVIRONMENT: str = config('ENVIRONMENT', 'dev') |
| 20 | + TESTING: bool = config('TESTING', False) |
| 21 | + SECRET_KEY: str = config('SECRET_KEY', None) |
| 22 | + |
| 23 | + # BACKEND_CORS_ORIGINS is a JSON-formatted list of origins |
| 24 | + # e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \ |
| 25 | + # "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]' |
| 26 | + BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] |
| 27 | + |
| 28 | + @validator('BACKEND_CORS_ORIGINS', pre=True) |
| 29 | + def assemble_cors_origins(cls, value: Union[str, List[str]]) -> Union[List[str], str]: |
| 30 | + """ |
| 31 | + Validate cors list |
| 32 | + """ |
| 33 | + if isinstance(value, str) and not value.startswith('['): |
| 34 | + return [i.strip() for i in value.split(',')] |
| 35 | + elif isinstance(value, (list, str)): |
| 36 | + return value |
| 37 | + raise ValueError(value) |
| 38 | + |
| 39 | + PROJECT_NAME: str = 'klipp' |
| 40 | + SENTRY_DSN: Optional[HttpUrl] = None |
| 41 | + |
| 42 | + @validator('SENTRY_DSN', pre=True) |
| 43 | + def sentry_dsn_can_be_blank(cls, value: str) -> Optional[str]: |
| 44 | + """ |
| 45 | + Validate sentry DSN |
| 46 | + """ |
| 47 | + if not value: |
| 48 | + return None |
| 49 | + return value |
| 50 | + |
| 51 | + class Config: # noqa |
| 52 | + case_sensitive = True |
| 53 | + |
| 54 | + |
| 55 | +@lru_cache |
| 56 | +def load_settings() -> Settings: |
| 57 | + """ |
| 58 | + Load all settings |
| 59 | + """ |
| 60 | + return Settings() |
0 commit comments