config.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import secrets
  2. from typing import Any, Dict, List, Optional, Union
  3. from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator
  4. class Settings(BaseSettings):
  5. API_V1_STR: str = "/api/v1"
  6. SECRET_KEY: str = secrets.token_urlsafe(32)
  7. # 60 minutes * 24 hours * 8 days = 8 days
  8. ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
  9. SERVER_NAME: str
  10. SERVER_HOST: AnyHttpUrl
  11. # BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
  12. # e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \
  13. # "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]'
  14. BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
  15. @validator("BACKEND_CORS_ORIGINS", pre=True)
  16. def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
  17. if isinstance(v, str) and not v.startswith("["):
  18. return [i.strip() for i in v.split(",")]
  19. elif isinstance(v, (list, str)):
  20. return v
  21. raise ValueError(v)
  22. PROJECT_NAME: str
  23. SENTRY_DSN: Optional[HttpUrl] = None
  24. @validator("SENTRY_DSN", pre=True)
  25. def sentry_dsn_can_be_blank(cls, v: str) -> Optional[str]:
  26. if len(v) == 0:
  27. return None
  28. return v
  29. POSTGRES_SERVER: str
  30. POSTGRES_USER: str
  31. POSTGRES_PASSWORD: str
  32. POSTGRES_DB: str
  33. SQLALCHEMY_DATABASE_URI: Optional[PostgresDsn] = None
  34. @validator("SQLALCHEMY_DATABASE_URI", pre=True)
  35. def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
  36. if isinstance(v, str):
  37. return v
  38. return PostgresDsn.build(
  39. scheme="postgresql",
  40. user=values.get("POSTGRES_USER"),
  41. password=values.get("POSTGRES_PASSWORD"),
  42. host=values.get("POSTGRES_SERVER"),
  43. path=f"/{values.get('POSTGRES_DB') or ''}",
  44. )
  45. SMTP_TLS: bool = True
  46. SMTP_PORT: Optional[int] = None
  47. SMTP_HOST: Optional[str] = None
  48. SMTP_USER: Optional[str] = None
  49. SMTP_PASSWORD: Optional[str] = None
  50. EMAILS_FROM_EMAIL: Optional[EmailStr] = None
  51. EMAILS_FROM_NAME: Optional[str] = None
  52. @validator("EMAILS_FROM_NAME")
  53. def get_project_name(cls, v: Optional[str], values: Dict[str, Any]) -> str:
  54. if not v:
  55. return values["PROJECT_NAME"]
  56. return v
  57. EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
  58. EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/build"
  59. EMAILS_ENABLED: bool = False
  60. @validator("EMAILS_ENABLED", pre=True)
  61. def get_emails_enabled(cls, v: bool, values: Dict[str, Any]) -> bool:
  62. return bool(
  63. values.get("SMTP_HOST")
  64. and values.get("SMTP_PORT")
  65. and values.get("EMAILS_FROM_EMAIL")
  66. )
  67. EMAIL_TEST_USER: EmailStr = "test@example.com" # type: ignore
  68. FIRST_SUPERUSER: EmailStr
  69. FIRST_SUPERUSER_PASSWORD: str
  70. USERS_OPEN_REGISTRATION: bool = False
  71. class Config:
  72. case_sensitive = True
  73. settings = Settings()