From ba68692b9d3d8bf9d9ee9127fe43671260b78025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 19 Mar 2022 02:28:18 +0000 Subject: [PATCH] more asyncio --- api/kiwi_vpn_api/config.py | 17 ++++------------- api/kiwi_vpn_api/main.py | 2 +- api/kiwi_vpn_api/routers/admin.py | 6 +++--- api/kiwi_vpn_api/routers/user.py | 2 +- 4 files changed, 9 insertions(+), 18 deletions(-) diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index bcd2497..10f4c06 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -35,7 +35,7 @@ class DBConfig(BaseModel): db_type: DBType = DBType.sqlite @property - def db_engine(self) -> Engine: + async def db_engine(self) -> Engine: return create_engine( "sqlite:///./tmp/vpn.db", connect_args={"check_same_thread": False}, @@ -99,7 +99,7 @@ class CryptoConfig(BaseModel): schemes: list[str] = ["bcrypt"] @property - def crypt_context(self) -> CryptContext: + async def crypt_context(self) -> CryptContext: return CryptContext( schemes=self.schemes, deprecated="auto", @@ -120,15 +120,6 @@ class Config(BaseModel): except FileNotFoundError: return None - @staticmethod - def set(config: Config) -> None: + async def set(self) -> None: with open(Settings.get().config_file, "w") as config_file: - config_file.write(config.json(indent=2)) - - @property - def crypt_context(self) -> CryptContext: - return self.crypto.crypt_context - - @property - def db_engine(self) -> Engine: - return self.db.db_engine + config_file.write(self.json(indent=2)) diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index dd4ee38..122e54b 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -36,7 +36,7 @@ api.include_router(user.router) @app.on_event("startup") async def on_startup() -> None: if (current_config := await Config.get()) is not None: - Connection.connect(current_config.db_engine) + Connection.connect(await current_config.db.db_engine) # some testing async for db in Connection.get(): diff --git a/api/kiwi_vpn_api/routers/admin.py b/api/kiwi_vpn_api/routers/admin.py index 68b2488..a73eee7 100644 --- a/api/kiwi_vpn_api/routers/admin.py +++ b/api/kiwi_vpn_api/routers/admin.py @@ -31,8 +31,8 @@ async def set_config( if new_config.jwt.secret is None: new_config.jwt.secret = token_hex(32) - Config.set(new_config) - Connection.connect(new_config.db_engine) + await new_config.set() + Connection.connect(await new_config.db.db_engine) @router.post( @@ -63,5 +63,5 @@ async def add_user( password=user_password, capabilities=["admin"], ), - crypt_context=current_config.crypt_context, + crypt_context=await current_config.crypto.crypt_context, ) diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index 777653b..977bd14 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -28,7 +28,7 @@ async def login( db=db, name=form_data.username, password=form_data.password, - crypt_context=config.crypt_context, + crypt_context=await config.crypto.crypt_context, ) if user is None: