mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-04 00:06:18 +00:00
Moved connection string to an individual file
This commit is contained in:
22
src/modules/ConnectionString.py
Normal file
22
src/modules/ConnectionString.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import configparser
|
||||
import os
|
||||
|
||||
from sqlalchemy_utils import database_exists, create_database
|
||||
|
||||
from DashboardConfig import DashboardConfig
|
||||
|
||||
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":
|
||||
cn = f'postgresql+psycopg2://{parser.get("Database", "username")}:{parser.get("Database", "password")[1]}@{parser.get("Database", "host")[1]}/{database}'
|
||||
elif parser.get("Database", "type")[1] == "mysql":
|
||||
cn = f'mysql+mysqldb://{parser.get("Database", "username")[1]}:{parser.get("Database", "password")[1]}@{parser.get("Database", "host")[1]}/{database}'
|
||||
else:
|
||||
cn = f'sqlite:///{os.path.join(sqlitePath, f"{database}.db")}'
|
||||
if not database_exists(cn):
|
||||
create_database(cn)
|
||||
return cn
|
3
src/modules/DashboardClient.py
Normal file
3
src/modules/DashboardClient.py
Normal file
@@ -0,0 +1,3 @@
|
||||
class DashboardClient:
|
||||
def __init__(self):
|
||||
pass
|
@@ -6,6 +6,8 @@ from sqlalchemy_utils import database_exists, create_database
|
||||
import sqlalchemy as db
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from .ConnectionString import ConnectionString
|
||||
from .Utilities import (
|
||||
GetRemoteEndpoint, ValidateDNSAddress
|
||||
)
|
||||
@@ -84,7 +86,7 @@ class DashboardConfig:
|
||||
if not exist:
|
||||
self.SetConfig(section, key, value, True)
|
||||
|
||||
self.engine = db.create_engine(self.getConnectionString('wgdashboard'))
|
||||
self.engine = db.create_engine(ConnectionString('wgdashboard'))
|
||||
self.dbMetadata = db.MetaData()
|
||||
self.__createAPIKeyTable()
|
||||
self.DashboardAPIKeys = self.__getAPIKeys()
|
||||
|
@@ -4,10 +4,12 @@ Dashboard Logger Class
|
||||
import uuid
|
||||
import sqlalchemy as db
|
||||
|
||||
from .ConnectionString import ConnectionString
|
||||
|
||||
|
||||
class DashboardLogger:
|
||||
def __init__(self, DashboardConfig):
|
||||
self.engine = db.create_engine(DashboardConfig.getConnectionString("wgdashboard_log"))
|
||||
self.engine = db.create_engine(ConnectionString("wgdashboard_log"))
|
||||
self.metadata = db.MetaData()
|
||||
self.dashboardLoggerTable = db.Table('DashboardLog', self.metadata,
|
||||
db.Column('LogID', db.String(255), nullable=False, primary_key=True),
|
||||
|
@@ -3,11 +3,13 @@ Peer Job Logger
|
||||
"""
|
||||
import uuid
|
||||
import sqlalchemy as db
|
||||
|
||||
from .ConnectionString import ConnectionString
|
||||
from .Log import Log
|
||||
|
||||
class PeerJobLogger:
|
||||
def __init__(self, AllPeerJobs, DashboardConfig):
|
||||
self.engine = db.create_engine(DashboardConfig.getConnectionString("wgdashboard_log"))
|
||||
self.engine = db.create_engine(ConnectionString("wgdashboard_log"))
|
||||
self.metadata = db.MetaData()
|
||||
self.jobLogTable = db.Table('JobLog', self.metadata,
|
||||
db.Column('LogID', db.String(255), nullable=False, primary_key=True),
|
||||
|
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Peer Jobs
|
||||
"""
|
||||
from .ConnectionString import ConnectionString
|
||||
from .PeerJob import PeerJob
|
||||
from .PeerJobLogger import PeerJobLogger
|
||||
import sqlalchemy as db
|
||||
@@ -9,7 +10,7 @@ from datetime import datetime
|
||||
class PeerJobs:
|
||||
def __init__(self, DashboardConfig, WireguardConfigurations):
|
||||
self.Jobs: list[PeerJob] = []
|
||||
self.engine = db.create_engine(DashboardConfig.getConnectionString('wgdashboard_job'))
|
||||
self.engine = db.create_engine(ConnectionString('wgdashboard_job'))
|
||||
self.metadata = db.MetaData()
|
||||
self.peerJobTable = db.Table('PeerJobs', self.metadata,
|
||||
db.Column('JobID', db.String(255), nullable=False, primary_key=True),
|
||||
|
@@ -1,3 +1,4 @@
|
||||
from .ConnectionString import ConnectionString
|
||||
from .PeerShareLink import PeerShareLink
|
||||
import sqlalchemy as db
|
||||
from datetime import datetime
|
||||
@@ -9,7 +10,7 @@ Peer Share Links
|
||||
class PeerShareLinks:
|
||||
def __init__(self, DashboardConfig):
|
||||
self.Links: list[PeerShareLink] = []
|
||||
self.engine = db.create_engine(DashboardConfig.getConnectionString("wgdashboard"))
|
||||
self.engine = db.create_engine(ConnectionString("wgdashboard"))
|
||||
self.metadata = db.MetaData()
|
||||
self.peerShareLinksTable = db.Table(
|
||||
'PeerShareLinks', self.metadata,
|
||||
|
@@ -6,6 +6,7 @@ from zipfile import ZipFile
|
||||
from datetime import datetime, timedelta
|
||||
from itertools import islice
|
||||
|
||||
from .ConnectionString import ConnectionString
|
||||
from .DashboardConfig import DashboardConfig
|
||||
from .Peer import Peer
|
||||
from .PeerJobs import PeerJobs
|
||||
@@ -56,7 +57,7 @@ class WireguardConfiguration:
|
||||
self.DashboardConfig = DashboardConfig
|
||||
self.AllPeerShareLinks = AllPeerShareLinks
|
||||
self.configPath = os.path.join(self.__getProtocolPath(), f'{self.Name}.conf')
|
||||
self.engine: sqlalchemy.engine = sqlalchemy.create_engine(self.DashboardConfig.getConnectionString("wgdashboard"))
|
||||
self.engine: sqlalchemy.engine = sqlalchemy.create_engine(ConnectionString("wgdashboard"))
|
||||
self.metadata: sqlalchemy.MetaData = sqlalchemy.MetaData()
|
||||
self.dbType = self.DashboardConfig.GetConfig("Database", "type")[1]
|
||||
|
||||
|
Reference in New Issue
Block a user