diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index 5c8672f..095b6c4 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -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}://" - f"{self.user}:{self.password}@{self.host}" - f"/{self.database}{args_str}", - pool_recycle=3600, - ) + return (f"mysql+{self.mysql_driver}://" + f"{self.user}:{self.password}@{self.host}" + f"/{self.database}{args_str}") class JWTConfig(BaseModel): diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index e560590..71870fb 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -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: diff --git a/api/kiwi_vpn_api/routers/admin.py b/api/kiwi_vpn_api/routers/admin.py index e856ea9..ce03583 100644 --- a/api/kiwi_vpn_api/routers/admin.py +++ b/api/kiwi_vpn_api/routers/admin.py @@ -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)