-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_schema.py
More file actions
106 lines (83 loc) · 3.49 KB
/
Copy pathconfig_schema.py
File metadata and controls
106 lines (83 loc) · 3.49 KB
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
from enum import StrEnum
from pathlib import Path
import yaml
from pydantic import BaseModel, ConfigDict, SecretStr
class Environment(StrEnum):
DEVELOPMENT = "development"
PRODUCTION = "production"
TESTING = "testing"
class SettingsEntityModel(BaseModel):
model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
class InnopolisSSO(SettingsEntityModel):
"""Innopolis SSO settings (only for production)"""
client_id: str
"Client ID for Innopolis SSO"
client_secret: SecretStr
"Client secret for Innopolis SSO"
redirect_uri: str
"Redirect URI for Innopolis SSO"
resource_id: str | None = None
"Resource ID for Innopolis SSO (optional); Used for Sports API access"
class Telegram(SettingsEntityModel):
bot_username: str
"Bot username for Telegram"
bot_token: SecretStr
"Bot token for Telegram"
class Accounts(SettingsEntityModel):
"""InNoHassle Accounts integration settings"""
api_url: str = "https://api.innohassle.ru/accounts/v0"
"API URL for InNoHassle Accounts"
api_jwt_token: SecretStr
"JWT token for accessing the Accounts API as a service"
class Authentication(SettingsEntityModel):
allowed_domains: list[str] = ["localhost", "127.0.0.1", "0.0.0.0"]
"Allowed domains for redirecting after authentication"
jwt_private_key: SecretStr
"Private key for JWT. Use 'openssl genrsa -out private.pem 2048' to generate keys"
jwt_public_key: str
"Public key for JWT. Use 'openssl rsa -in private.pem -pubout -out public.pem' to generate keys"
session_secret_key: SecretStr
"Secret key for sessions. Use 'openssl rand -hex 32' to generate keys"
class Mongo(SettingsEntityModel):
uri: SecretStr
"MongoDB database connection URI"
class Settings(SettingsEntityModel):
"""
Settings for the application. Get settings from `settings.yaml` file.
"""
app_root_path: str = ""
"Prefix for the API path (e.g. '/api/v0')"
environment: Environment = Environment.DEVELOPMENT
"App environment"
mongo: Mongo
"MongoDB settings"
web_url: str
"Web URL for the frontend part of InNoHassle-Accounts"
cors_allow_origins_regex: str = r"^(https://innohassle\.ru|https://\w+\.innohassle\.ru|https://local\.innohassle\.ru(:30[0-9][0-9])?|http://localhost(:30[0-9][0-9])?:http://127\.0\.0\.1(:30[0-9][0-9])?)$"
"Allowed origins for CORS: from which domains requests to the API are allowed"
auth: Authentication
"Authentication settings"
innopolis_sso: InnopolisSSO | None = None
"Innopolis SSO settings (only for production)"
telegram: Telegram | None = None
"Telegram settings"
proxy_url: str | None = None
"Proxy for telegram"
accounts: Accounts | None = None
"Use production InNoHassle Accounts API for authentication in local development"
@classmethod
def from_yaml(cls, path: Path) -> "Settings":
with open(path, encoding="utf-8") as f:
yaml_config: dict = yaml.safe_load(f)
yaml_config.pop("$schema", None)
return cls.parse_obj(yaml_config)
@classmethod
def save_schema(cls, path: Path) -> None:
with open(path, "w", encoding="utf-8") as f:
schema = {"$schema": "https://json-schema.org/draft-07/schema", **cls.model_json_schema()}
schema["properties"]["$schema"] = {
"description": "Path to the schema file",
"title": "Schema",
"type": "string",
}
yaml.dump(schema, f, sort_keys=False)