Funktionsverschiebungen 1.1
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
from .setup_routes import setup_blueprint
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import os
|
|
||||||
import json
|
|
||||||
import mysql.connector
|
|
||||||
from util.logger import logger
|
|
||||||
|
|
||||||
CONFIG_PATH = "config/db_config.json"
|
|
||||||
|
|
||||||
def is_configured():
|
|
||||||
return os.path.exists(CONFIG_PATH)
|
|
||||||
|
|
||||||
def load_config():
|
|
||||||
try:
|
|
||||||
with open(CONFIG_PATH, "r") as file:
|
|
||||||
return json.load(file)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"[DB-Config] Fehler beim Laden: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def save_config(config):
|
|
||||||
try:
|
|
||||||
os.makedirs("config", exist_ok=True)
|
|
||||||
with open(CONFIG_PATH, "w") as file:
|
|
||||||
json.dump(config, file, indent=2)
|
|
||||||
logger.info("[DB-Config] Konfiguration gespeichert.")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"[DB-Config] Fehler beim Speichern: {e}")
|
|
||||||
|
|
||||||
def test_connection(config) -> bool:
|
|
||||||
try:
|
|
||||||
conn = mysql.connector.connect(**config)
|
|
||||||
conn.close()
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"[DB-Check] Verbindung fehlgeschlagen: {e}")
|
|
||||||
return False
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
from util import setup_routes
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
# util/db_config.py
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import mysql.connector
|
||||||
|
from util.logger import logger
|
||||||
|
|
||||||
|
CONFIG_PATH = "./config/db_config.json"
|
||||||
|
|
||||||
|
def load_config():
|
||||||
|
try:
|
||||||
|
with open(CONFIG_PATH, "r") as f:
|
||||||
|
return json.load(f)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to load database config: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def save_config(data):
|
||||||
|
try:
|
||||||
|
os.makedirs(os.path.dirname(CONFIG_PATH), exist_ok=True)
|
||||||
|
with open(CONFIG_PATH, "w") as f:
|
||||||
|
json.dump(data, f, indent=2)
|
||||||
|
logger.info("Database config saved.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to save database config: {e}")
|
||||||
|
|
||||||
|
def test_connection(config):
|
||||||
|
try:
|
||||||
|
conn = mysql.connector.connect(**config)
|
||||||
|
conn.close()
|
||||||
|
logger.info("Database connection successful.")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Database connection failed: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def initialize_admin_user(config):
|
||||||
|
try:
|
||||||
|
conn = mysql.connector.connect(**config)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
username VARCHAR(50) NOT NULL UNIQUE,
|
||||||
|
password VARCHAR(255) NOT NULL,
|
||||||
|
role VARCHAR(20) NOT NULL
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
cursor.execute("SELECT COUNT(*) FROM users WHERE username = 'admin'")
|
||||||
|
if cursor.fetchone()[0] == 0:
|
||||||
|
from werkzeug.security import generate_password_hash
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO users (username, password, role) VALUES (%s, %s, %s)",
|
||||||
|
("admin", generate_password_hash("admin"), "admin")
|
||||||
|
)
|
||||||
|
logger.info("Admin user created (username: admin, password: admin)")
|
||||||
|
conn.commit()
|
||||||
|
cursor.close()
|
||||||
|
conn.close()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to initialize admin user: {e}")
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
from flask import Blueprint, request, render_template, redirect, jsonify
|
from flask import Blueprint, request, render_template, redirect, jsonify
|
||||||
from config.db_config import load_config, save_config, test_connection, is_configured
|
from backend.util.db_config import load_config, save_config, test_connection, is_configured
|
||||||
from auth.setup_admin import initialize_admin_user
|
from auth.setup_admin import initialize_admin_user
|
||||||
from util.logger import logger
|
from util.logger import logger
|
||||||
|
|
||||||
Reference in New Issue
Block a user