Config.load_sync -> Config._

This commit is contained in:
Jörn-Michael Miehe 2022-03-28 02:15:42 +00:00
parent b7179e7cfc
commit 3d83ddb6cc
4 changed files with 16 additions and 9 deletions

View file

@ -196,15 +196,22 @@ class Config(BaseModel):
jwt: JWTConfig = Field(default_factory=JWTConfig) jwt: JWTConfig = Field(default_factory=JWTConfig)
crypto: CryptoConfig = Field(default_factory=CryptoConfig) crypto: CryptoConfig = Field(default_factory=CryptoConfig)
@staticmethod __singleton: Config | None = None
def load_sync() -> Config | None:
@classmethod
@property
def _(cls) -> Config | None:
""" """
Load configuration from config file Load configuration from config file
""" """
if cls.__singleton is not None:
return cls.__singleton
try: try:
with open(Settings.get().config_file, "r") as config_file: with open(Settings.get().config_file, "r") as config_file:
return Config.parse_obj(json.load(config_file)) cls.__singleton = Config.parse_obj(json.load(config_file))
return cls.__singleton
except FileNotFoundError: except FileNotFoundError:
return None return None
@ -222,7 +229,7 @@ class Config(BaseModel):
except FileNotFoundError: except FileNotFoundError:
return None return None
async def save(self) -> None: def save(self) -> None:
""" """
Save configuration to config file Save configuration to config file
""" """

View file

@ -77,7 +77,7 @@ class User(UserBase, table=True):
Authenticate with name/password against users in database. Authenticate with name/password against users in database.
""" """
crypt_context = Config.load_sync().crypto.crypt_context_sync crypt_context = Config._.crypto.crypt_context_sync
if (user := cls.get(name)) is None: if (user := cls.get(name)) is None:
# nonexistent user, fake doing password verification # nonexistent user, fake doing password verification
@ -141,7 +141,7 @@ class UserCreate(UserBase):
if (password_clear := values.get("password_clear")) is None: if (password_clear := values.get("password_clear")) is None:
raise ValueError("No password to hash") raise ValueError("No password to hash")
if (current_config := Config.load_sync()) is None: if (current_config := Config._) is None:
raise ValueError("Not configured") raise ValueError("Not configured")
values["password"] = current_config.crypto.crypt_context_sync.hash( values["password"] = current_config.crypto.crypt_context_sync.hash(

View file

@ -41,7 +41,7 @@ app.include_router(main_router)
@app.on_event("startup") @app.on_event("startup")
async def on_startup() -> None: async def on_startup() -> None:
# check if configured # check if configured
if (current_config := await Config.load()) is not None: if (current_config := Config._) is not None:
# connect to database # connect to database
Connection.connect(current_config.db.uri) Connection.connect(current_config.db.uri)

View file

@ -33,7 +33,7 @@ async def initial_configure(
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
# create config file, connect to database # create config file, connect to database
await config.save() config.save()
Connection.connect(current_config.db.uri) Connection.connect(current_config.db.uri)
@ -90,5 +90,5 @@ async def set_config(
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
# update config file, reconnect to database # update config file, reconnect to database
await new_config.save() new_config.save()
Connection.connect(current_config.db.uri) Connection.connect(current_config.db.uri)