Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""This module defines the ESAP-DB configuration instance."""
import secrets
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator
class Settings(BaseSettings):
"""The class holding the configuration settings."""
API_V0_STR: str = '/api/v0'
SECRET_KEY: str = secrets.token_urlsafe(32)
# 60 minutes * 24 hours * 8 days = 8 days
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
SERVER_NAME: str
SERVER_HOST: AnyHttpUrl
# BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
# e.g: ['http://localhost', 'http://localhost:4200', 'http://localhost:3000', \
# 'http://localhost:8080']
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
@validator('BACKEND_CORS_ORIGINS', pre=True)
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
"""Validates the CORS origins."""
if isinstance(v, str) and not v.startswith('['):
return [i.strip() for i in v.split(',')]
elif isinstance(v, (list, str)):
return v
raise ValueError(v)
PROJECT_NAME: str
SENTRY_DSN: Optional[HttpUrl] = None
@validator('SENTRY_DSN', pre=True)
def sentry_dsn_can_be_blank(cls, v: str) -> Optional[str]:
"""Validates the sentry DSN."""
if len(v) == 0:
return None
return v
DBADMIN_SERVER: str
DBADMIN_USER: str
DBADMIN_PASSWORD: str
DBADMIN_DB: str
SQLALCHEMY_DBADMIN_URI: Optional[PostgresDsn] = None
@validator('SQLALCHEMY_DBADMIN_URI', pre=True, always=True)
def assemble_dbadmin_connection(
cls, v: Optional[str], values: Dict[str, Any]
) -> Any:
"""Validates the admin database URI."""
if isinstance(v, str):
return v
return PostgresDsn.build(
scheme='postgresql',
user=values.get('DBADMIN_USER'),
password=values.get('DBADMIN_PASSWORD'),
host=values.get('DBADMIN_SERVER'),
path=f"/{values.get('DBADMIN_DB')}",
)
DBPROJECT_USER: str
DBPROJECT_PASSWORD: str
DBPROJECT_SERVERS: list[str]
FIRST_SUPERUSER: EmailStr
FIRST_SUPERUSER_PASSWORD: str
class Config:
case_sensitive = True
settings = Settings(_env_file=Path(__file__).parents[1] / '.env') # type: ignore