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 jose.constants import ALGORITHMS
from passlib.context import CryptContext from passlib.context import CryptContext
from pydantic import BaseModel, BaseSettings, Field, validator from pydantic import BaseModel, BaseSettings, Field, validator
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
class Settings(BaseSettings): class Settings(BaseSettings):
@ -69,17 +67,14 @@ class DBConfig(BaseModel):
mysql_args: list[str] = ["charset=utf8mb4"] mysql_args: list[str] = ["charset=utf8mb4"]
@property @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: if self.type is DBType.sqlite:
# SQLite backend # SQLite backend
return create_engine( return f"sqlite:///{self.database}"
f"sqlite:///{self.database}",
connect_args={"check_same_thread": False},
)
elif self.type is DBType.mysql: elif self.type is DBType.mysql:
# MySQL backend # MySQL backend
@ -88,12 +83,9 @@ class DBConfig(BaseModel):
else: else:
args_str = "" args_str = ""
return create_engine( return (f"mysql+{self.mysql_driver}://"
f"mysql+{self.mysql_driver}://" f"{self.user}:{self.password}@{self.host}"
f"{self.user}:{self.password}@{self.host}" f"/{self.database}{args_str}")
f"/{self.database}{args_str}",
pool_recycle=3600,
)
class JWTConfig(BaseModel): class JWTConfig(BaseModel):

View file

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

View file

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