From d66ce5a026a9f0ae6a7e831cde1ef613766fd72f 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 03:31:15 +0000 Subject: [PATCH] add mysql support --- api/kiwi_vpn_api/config.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index bc7b45c..017f2ed 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -32,14 +32,37 @@ class DBType(Enum): class DBConfig(BaseModel): - db_type: DBType = DBType.sqlite + type: DBType = DBType.sqlite + user: str | None = None + password: str | None = None + host: str | None = None + database: str | None = "./tmp/vpn.db" + + mysql_driver: str = "pymysql" + mysql_args: list[str] = ["charset=utf8mb4"] @property async def db_engine(self) -> Engine: - return create_engine( - "sqlite:///./tmp/vpn.db", - connect_args={"check_same_thread": False}, - ) + if self.type is DBType.sqlite: + # SQLite backend + return create_engine( + f"sqlite:///{self.database}", + connect_args={"check_same_thread": False}, + ) + + elif self.type is DBType.mysql: + # MySQL backend + if self.mysql_args: + args_str = "?" + "&".join(self.mysql_args) + 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, + ) class JWTConfig(BaseModel):