2025-06-01 15:34:12 +08:00
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
from sqlalchemy_utils import database_exists, create_database
|
|
|
|
def ConnectionString(database) -> str or None:
|
|
|
|
parser = configparser.ConfigParser(strict=False)
|
|
|
|
parser.read_file(open('wg-dashboard.ini', "r+"))
|
|
|
|
sqlitePath = os.path.join("db")
|
|
|
|
if not os.path.isdir(sqlitePath):
|
|
|
|
os.mkdir(sqlitePath)
|
|
|
|
if parser.get("Database", "type") == "postgresql":
|
2025-06-02 12:04:01 +08:00
|
|
|
cn = f'postgresql+psycopg2://{parser.get("Database", "username")}:{parser.get("Database", "password")}@{parser.get("Database", "host")}/{database}'
|
|
|
|
elif parser.get("Database", "type") == "mysql":
|
|
|
|
cn = f'mysql+mysqldb://{parser.get("Database", "username")}:{parser.get("Database", "password")}@{parser.get("Database", "host")}/{database}'
|
2025-06-01 15:34:12 +08:00
|
|
|
else:
|
|
|
|
cn = f'sqlite:///{os.path.join(sqlitePath, f"{database}.db")}'
|
2025-06-19 00:41:08 +08:00
|
|
|
try:
|
|
|
|
if not database_exists(cn):
|
|
|
|
create_database(cn)
|
|
|
|
except Exception as e:
|
|
|
|
print("[WGDashboard] Database error: " + str(e))
|
|
|
|
exit(1)
|
|
|
|
|
2025-06-01 15:34:12 +08:00
|
|
|
return cn
|