more asyncio

This commit is contained in:
Jörn-Michael Miehe 2022-03-19 02:28:18 +00:00
parent 6e9cf9f920
commit ba68692b9d
4 changed files with 9 additions and 18 deletions

View file

@ -35,7 +35,7 @@ class DBConfig(BaseModel):
db_type: DBType = DBType.sqlite db_type: DBType = DBType.sqlite
@property @property
def db_engine(self) -> Engine: async def db_engine(self) -> Engine:
return create_engine( return create_engine(
"sqlite:///./tmp/vpn.db", "sqlite:///./tmp/vpn.db",
connect_args={"check_same_thread": False}, connect_args={"check_same_thread": False},
@ -99,7 +99,7 @@ class CryptoConfig(BaseModel):
schemes: list[str] = ["bcrypt"] schemes: list[str] = ["bcrypt"]
@property @property
def crypt_context(self) -> CryptContext: async def crypt_context(self) -> CryptContext:
return CryptContext( return CryptContext(
schemes=self.schemes, schemes=self.schemes,
deprecated="auto", deprecated="auto",
@ -120,15 +120,6 @@ class Config(BaseModel):
except FileNotFoundError: except FileNotFoundError:
return None return None
@staticmethod async def set(self) -> None:
def set(config: Config) -> None:
with open(Settings.get().config_file, "w") as config_file: with open(Settings.get().config_file, "w") as config_file:
config_file.write(config.json(indent=2)) config_file.write(self.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

View file

@ -36,7 +36,7 @@ api.include_router(user.router)
@app.on_event("startup") @app.on_event("startup")
async def on_startup() -> None: async def on_startup() -> None:
if (current_config := await Config.get()) is not 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 # some testing
async for db in Connection.get(): async for db in Connection.get():

View file

@ -31,8 +31,8 @@ async def set_config(
if new_config.jwt.secret is None: if new_config.jwt.secret is None:
new_config.jwt.secret = token_hex(32) new_config.jwt.secret = token_hex(32)
Config.set(new_config) await new_config.set()
Connection.connect(new_config.db_engine) Connection.connect(await new_config.db.db_engine)
@router.post( @router.post(
@ -63,5 +63,5 @@ async def add_user(
password=user_password, password=user_password,
capabilities=["admin"], capabilities=["admin"],
), ),
crypt_context=current_config.crypt_context, crypt_context=await current_config.crypto.crypt_context,
) )

View file

@ -28,7 +28,7 @@ async def login(
db=db, db=db,
name=form_data.username, name=form_data.username,
password=form_data.password, password=form_data.password,
crypt_context=config.crypt_context, crypt_context=await config.crypto.crypt_context,
) )
if user is None: if user is None: