use correct database URI

This commit is contained in:
Jörn-Michael Miehe 2022-03-28 02:00:58 +00:00
parent ae8894f5cc
commit 19dd5aaee7
3 changed files with 9 additions and 17 deletions

View file

@ -20,8 +20,6 @@ from jose import JWTError, jwt
from jose.constants import ALGORITHMS
from passlib.context import CryptContext
from pydantic import BaseModel, BaseSettings, Field, validator
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
class Settings(BaseSettings):
@ -69,17 +67,14 @@ class DBConfig(BaseModel):
mysql_args: list[str] = ["charset=utf8mb4"]
@property
async def db_engine(self) -> Engine:
def uri(self) -> str:
"""
Construct an SQLAlchemy engine
Construct a database connection string
"""
if self.type is DBType.sqlite:
# SQLite backend
return create_engine(
f"sqlite:///{self.database}",
connect_args={"check_same_thread": False},
)
return f"sqlite:///{self.database}"
elif self.type is DBType.mysql:
# MySQL backend
@ -88,12 +83,9 @@ class DBConfig(BaseModel):
else:
args_str = ""
return create_engine(
f"mysql+{self.mysql_driver}://"
return (f"mysql+{self.mysql_driver}://"
f"{self.user}:{self.password}@{self.host}"
f"/{self.database}{args_str}",
pool_recycle=3600,
)
f"/{self.database}{args_str}")
class JWTConfig(BaseModel):

View file

@ -44,7 +44,7 @@ async def on_startup() -> None:
# check if configured
if (current_config := await Config.load()) is not None:
# connect to database
Connection.connect("sqlite:///tmp/vpn.db")
Connection.connect(current_config.db.uri)
# # some testing
# with Connection.use() as db:

View file

@ -34,7 +34,7 @@ async def initial_configure(
# create config file, connect to database
await config.save()
Connection.connect("sqlite:///tmp/vpn.db")
Connection.connect(current_config.db.uri)
@router.put(
@ -91,4 +91,4 @@ async def set_config(
# update config file, reconnect to database
await new_config.save()
Connection.connect("sqlite:///tmp/vpn.db")
Connection.connect(current_config.db.uri)