mirror of
https://github.com/WGDashboard/WGDashboard-PRW.git
synced 2026-08-04 06:52:58 +00:00
chore: try to make a working base
This commit is contained in:
@@ -1,2 +1,43 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import logging as log
|
||||
|
||||
import sqlalchemy
|
||||
import sqlalchemy.orm
|
||||
|
||||
from .schema import Base
|
||||
from .utilities import checks
|
||||
|
||||
class database():
|
||||
@staticmethod
|
||||
def create_session(database_config: dict) -> tuple[bool, sqlalchemy.engine.Engine | None, sqlalchemy.orm.Session | None]:
|
||||
ok, connection_string = checks.generate_connection_string(database_config)
|
||||
if not ok:
|
||||
return False, None, None
|
||||
|
||||
log.info(connection_string)
|
||||
|
||||
try:
|
||||
engine = sqlalchemy.create_engine(connection_string, echo=False)
|
||||
|
||||
local_session = sqlalchemy.orm.sessionmaker(bind=engine)
|
||||
session = local_session()
|
||||
|
||||
return True, engine, session
|
||||
|
||||
except Exception as err:
|
||||
log.critical(f'database initialization failed: {err}')
|
||||
return False, None, None
|
||||
|
||||
@staticmethod
|
||||
def verify_contents(engine) -> bool:
|
||||
try:
|
||||
log.info('checking if all tables are present, and creating them if they are not')
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as err:
|
||||
log.error('failed to verify contents of the database')
|
||||
return False
|
||||
|
||||
@@ -1,2 +1,25 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import sqlalchemy
|
||||
import sqlalchemy.orm
|
||||
|
||||
Base = sqlalchemy.orm.declarative_base()
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'users'
|
||||
|
||||
user_id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, index=True)
|
||||
|
||||
username = sqlalchemy.Column(sqlalchemy.String, unique=True, index=True)
|
||||
password = sqlalchemy.Column(sqlalchemy.String)
|
||||
|
||||
role = sqlalchemy.Column(sqlalchemy.String, default='user')
|
||||
|
||||
totp_enabled = sqlalchemy.Column(sqlalchemy.Boolean, default=False)
|
||||
totp_verified = sqlalchemy.Column(sqlalchemy.Boolean, default=False)
|
||||
totp_key = sqlalchemy.Column(sqlalchemy.String)
|
||||
|
||||
email = sqlalchemy.Column(sqlalchemy.String)
|
||||
|
||||
class Wireguard(Base):
|
||||
__tablename__ = 'wireguard_interfaces'
|
||||
@@ -1,9 +1,45 @@
|
||||
#!/bin/env python3
|
||||
|
||||
class db_setup():
|
||||
import logging as log
|
||||
|
||||
import os
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
from modules.utilities.utilities import utilities as util
|
||||
|
||||
class checks():
|
||||
'''
|
||||
This class functions as a collection of function to prepare a connection to a database.
|
||||
'''
|
||||
|
||||
def compile_connection_string() -> string:
|
||||
return "The Connection string"
|
||||
@staticmethod
|
||||
def generate_connection_string(database_config: dict) -> tuple[bool, str]:
|
||||
if not 'type' in database_config:
|
||||
return False, ''
|
||||
|
||||
username = quote_plus(database_config.get('username', ''))
|
||||
password = quote_plus(database_config.get('password', ''))
|
||||
|
||||
match database_config['type']:
|
||||
case 'sqlite':
|
||||
local_database_path = os.path.abspath("./database")
|
||||
|
||||
exists = util.ensure_directory(local_database_path)
|
||||
if exists:
|
||||
connection_string = f'sqlite:///{local_database_path}/wgdashboard.db'
|
||||
else:
|
||||
return False, ''
|
||||
|
||||
case 'postgresql' | 'postgres':
|
||||
connection_string = f'postgresql+psycopg://{username}:{password}@{database_config.get('host', 'localhost')}:{database_config.get('port', '5432')}'
|
||||
|
||||
case 'mariadb':
|
||||
connection_string = f'mariadb+mariadbconnector://{username}:{password}@{database_config.get('host', 'localhost')}:{database_config.get('port', '3306')}'
|
||||
|
||||
case 'mysql':
|
||||
connection_string = f'mysql+pymysql://{username}:{password}@{database_config.get('host', 'localhost')}:{database_config.get('port', '3306')}'
|
||||
|
||||
case _:
|
||||
return False, ''
|
||||
|
||||
return True, connection_string
|
||||
Reference in New Issue
Block a user