-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
41 lines (31 loc) · 1.11 KB
/
config.py
File metadata and controls
41 lines (31 loc) · 1.11 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
from orjson import dumps, loads, OPT_INDENT_2
from pydantic import BaseModel
from os import urandom
from typing import Optional
class MongoDBConfig(BaseModel):
uri: str = "mongodb://username:password@example.com/hspc"
db_name: str = "hspc"
use_tls: bool = False
tls_cafile: Optional[str] = None
class Config(BaseModel):
host: str = "0.0.0.0"
port: int = 8080
jwt_key: str = urandom(16).hex()
allow_origins: list[str] = []
mongodb_config: MongoDBConfig = MongoDBConfig()
if __name__ == "config":
try:
with open("config.json", "rb") as config_file:
config = Config(**loads(config_file.read()))
except:
config = Config()
HOST = config.host
PORT = config.port
JWT_KEY = config.jwt_key
ALLOW_ORIGINS = config.allow_origins
MONGODB_URI = config.mongodb_config.uri
MONGODB_NAME = config.mongodb_config.db_name
MONGODB_TLS = config.mongodb_config.use_tls
MONGODB_CAFILE = config.mongodb_config.tls_cafile
with open("config.json", "wb") as config_file:
config_file.write(dumps(config.model_dump(), option=OPT_INDENT_2))