-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_schema.py
57 lines (43 loc) · 1.77 KB
/
config_schema.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
54
55
56
57
from enum import StrEnum
from pathlib import Path
import yaml
from pydantic import BaseModel, ConfigDict, Field, SecretStr
class Environment(StrEnum):
DEVELOPMENT = "development"
PRODUCTION = "production"
class SettingBaseModel(BaseModel):
model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
class Accounts(SettingBaseModel):
"""InNoHassle Accounts integration settings"""
api_url: str = "https://api.innohassle.ru/accounts/v0"
"URL of the Accounts API"
api_jwt_token: SecretStr
"JWT token for accessing the Accounts API as a service"
class Settings(SettingBaseModel):
"""Settings for the application."""
schema_: str = Field(None, alias="$schema")
environment: Environment = Environment.DEVELOPMENT
"App environment flag"
app_root_path: str = ""
'Prefix for the API path (e.g. "/api/v0")'
database_uri: SecretStr = Field(
examples=[
"mongodb://mongoadmin:secret@localhost:27017/db?authSource=admin",
"mongodb://mongoadmin:secret@db:27017/db?authSource=admin",
]
)
"MongoDB database settings"
cors_allow_origin_regex: str = ".*"
"Allowed origins for CORS: from which domains requests to the API are allowed. Specify as a regex: `https://.*.innohassle.ru`"
accounts: Accounts
"InNoHassle Accounts integration settings"
@classmethod
def from_yaml(cls, path: Path) -> "Settings":
with open(path) as f:
yaml_config = yaml.safe_load(f)
return cls.model_validate(yaml_config)
@classmethod
def save_schema(cls, path: Path) -> None:
with open(path, "w") as f:
schema = {"$schema": "https://json-schema.org/draft-07/schema", **cls.model_json_schema()}
yaml.dump(schema, f, sort_keys=False)