-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfiguration.py
53 lines (40 loc) · 1.35 KB
/
configuration.py
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
from __future__ import annotations
import os
from functools import lru_cache
from typing import Type, List, Union
import yaml
from pydantic import BaseSettings, AnyHttpUrl, validator
# Configuration Objects Definitions
class GlobalSettings(BaseSettings):
app_name: str = "My Awesome API"
environment: str = "TEST"
port: int = 80
swagger_path: str = "/docs"
cors: Union[List[str], List[AnyHttpUrl]] = []
@validator("cors", pre=True)
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
if v is None:
print("CORS Not Specified")
return []
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)
class Config:
env_file = "TEST.env"
# Utils to load Configurations
def __load_env_file_on_settings(settings: Type[BaseSettings]):
env = os.environ.get("ENV")
if env:
return settings(_env_file="{}.env".format(env))
else:
return settings()
@lru_cache()
def get_global_settings() -> GlobalSettings:
return __load_env_file_on_settings(GlobalSettings)
@lru_cache()
def get_log_config():
with open("logging.yaml") as logconf:
log_config = yaml.safe_load(logconf)
return log_config