File size: 3,226 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import enum
import os
from typing import List, Optional
import yaml

from pydantic import (
    AnyHttpUrl,
    BaseSettings,
    EmailStr,
    PostgresDsn,
)


class Settings(BaseSettings):
    API_V1_STR: str
    SECRET_KEY: str
    SESSION_EXPIRE_TIME: int
    SESSION_EXPIRE_TIME_EXTENDED: int
    TWO_FA_TIMEOUT: int
    PASSWORD_LESS_CREATE_TIMEOUT: int
    SERVER_NAME: str
    PROTOCOL = "http"

    #PROTOCAL: str
    MODE: str

    BACKEND_HOST: str
    BACKEND_PORT: int

    @property
    def BACKEND_URL_BASE(self):
        if self.BACKEND_PORT == 80:
            return f"{self.PROTOCOL}://{self.BACKEND_HOST}"
        else:
            return f"{self.PROTOCOL}://{self.BACKEND_HOST}:{self.BACKEND_PORT}"

    STATIC_HOST: str
    STATIC_PORT: int
    

    @property
    def STATIC_URL_BASE(self):
        if self.STATIC_PORT == 80:
            return f"{self.PROTOCOL}://{self.STATIC_HOST}"
        else:
            return f"{self.PROTOCOL}://{self.STATIC_HOST}:{self.STATIC_PORT}"

    FRONTEND_HOST: str
    FRONTEND_PORT: int

    @property
    def FRONTEND_URL_BASE(self):
        if self.FRONTEND_PORT == 80:
            return f"{self.PROTOCOL}://{self.FRONTEND_HOST}"
        else:
            return f"{self.PROTOCOL}://{self.FRONTEND_HOST}:{self.FRONTEND_PORT}"

    UPLOAD_DIR_ROOT: str

    WORKERS: int

    @property
    def DEV_MODE(self):
        if self.MODE == "dev":
            return True
        else:
            return False

    BACKEND_CORS_ORIGINS: List[AnyHttpUrl]

    ALLOWED_EMAIL_HOST: List[str]

    PROJECT_NAME: str

    POSTGRES_SERVER: str
    POSTGRES_USER: str
    POSTGRES_PASSWORD: str
    POSTGRES_DB: str

    @property
    def POSTGRES_DATABASE_URI(self):
        return PostgresDsn.build(
            scheme="postgresql",
            user=self.POSTGRES_USER,
            password=self.POSTGRES_PASSWORD,
            host=self.POSTGRES_SERVER,
            path=f"/{self.POSTGRES_DB or ''}",
        )

    REDIS_HOST: str
    REDIS_PORT: str
    REDIS_PASSWORD: str

    SMTP_TLS: bool
    SMTP_PORT: Optional[int]
    SMTP_HOST: Optional[str]
    SMTP_USER: Optional[str]
    SMTP_PASSWORD: Optional[str]

    EMAILS_FROM_EMAIL: Optional[EmailStr]

    @property
    def EMAILS_FROM_NAME(self):
        return self.PROJECT_NAME

    EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
    EMAIL_VERIFY_EXPIRE_HOURS: int = 48
    EMAIL_TEMPLATES_DIR: str

    @property
    def EMAILS_ENABLED(self):
        return bool(
            self.SMTP_HOST
            and self.SMTP_PORT
            and self.EMAILS_FROM_EMAIL
        )

    FIRST_SUPERUSER: EmailStr
    FIRST_SUPERUSER_PASSWORD: str
    USERS_OPEN_REGISTRATION: bool

    class UserType(enum.Enum):
        SUPERADMIN: int = 1
        ADMIN: int = 2
        TEACHER: int = 3
        STUDENT: int = 4

    class Config:
        case_sensitive = True


configs = {}

with open("etc/base.yml", "r") as base_config_file:
    configs = yaml.load(base_config_file.read(), yaml.Loader)

config_path = os.environ.get("CONFIG_PATH") or "etc/dev.yml"

with open(config_path, "r") as config_file:
    custom_configs = yaml.load(config_file.read(), yaml.Loader)
    configs.update(custom_configs)

settings = Settings(**configs)