From 3d83ddb6cc2838c5c68129ce727256d9f619cab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 28 Mar 2022 02:15:42 +0000 Subject: [PATCH] Config.load_sync -> Config._ --- api/kiwi_vpn_api/config.py | 15 +++++++++++---- api/kiwi_vpn_api/db/user.py | 4 ++-- api/kiwi_vpn_api/main.py | 2 +- api/kiwi_vpn_api/routers/admin.py | 4 ++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index 095b6c4..b90c103 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -196,15 +196,22 @@ class Config(BaseModel): jwt: JWTConfig = Field(default_factory=JWTConfig) crypto: CryptoConfig = Field(default_factory=CryptoConfig) - @staticmethod - def load_sync() -> Config | None: + __singleton: Config | None = None + + @classmethod + @property + def _(cls) -> Config | None: """ Load configuration from config file """ + if cls.__singleton is not None: + return cls.__singleton + try: 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: return None @@ -222,7 +229,7 @@ class Config(BaseModel): except FileNotFoundError: return None - async def save(self) -> None: + def save(self) -> None: """ Save configuration to config file """ diff --git a/api/kiwi_vpn_api/db/user.py b/api/kiwi_vpn_api/db/user.py index 2e53964..f13ccc1 100644 --- a/api/kiwi_vpn_api/db/user.py +++ b/api/kiwi_vpn_api/db/user.py @@ -77,7 +77,7 @@ class User(UserBase, table=True): 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: # nonexistent user, fake doing password verification @@ -141,7 +141,7 @@ class UserCreate(UserBase): if (password_clear := values.get("password_clear")) is None: raise ValueError("No password to hash") - if (current_config := Config.load_sync()) is None: + if (current_config := Config._) is None: raise ValueError("Not configured") values["password"] = current_config.crypto.crypt_context_sync.hash( diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index 4503d1a..594d948 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -41,7 +41,7 @@ app.include_router(main_router) @app.on_event("startup") async def on_startup() -> None: # check if configured - if (current_config := await Config.load()) is not None: + if (current_config := Config._) is not None: # connect to database Connection.connect(current_config.db.uri) diff --git a/api/kiwi_vpn_api/routers/admin.py b/api/kiwi_vpn_api/routers/admin.py index 99dfdeb..64c7307 100644 --- a/api/kiwi_vpn_api/routers/admin.py +++ b/api/kiwi_vpn_api/routers/admin.py @@ -33,7 +33,7 @@ async def initial_configure( raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) # create config file, connect to database - await config.save() + config.save() Connection.connect(current_config.db.uri) @@ -90,5 +90,5 @@ async def set_config( raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) # update config file, reconnect to database - await new_config.save() + new_config.save() Connection.connect(current_config.db.uri)