Funktionsverschiebungen 1.1
This commit is contained in:
@@ -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}")
|
||||
@@ -0,0 +1,47 @@
|
||||
import time
|
||||
import os
|
||||
from flask import Blueprint, request, render_template, redirect, jsonify
|
||||
from backend.util.db_config import load_config, save_config, test_connection, is_configured
|
||||
from auth.setup_admin import initialize_admin_user
|
||||
from util.logger import logger
|
||||
|
||||
setup_blueprint = Blueprint("setup", __name__, template_folder="../backend/templates")
|
||||
|
||||
MAX_WAIT = 30
|
||||
WAIT_INTERVAL = 10
|
||||
|
||||
def wait_for_db():
|
||||
elapsed = 0
|
||||
config = load_config()
|
||||
while not test_connection(config) and elapsed < MAX_WAIT:
|
||||
logger.info(f"[SETUP] DB nicht erreichbar – warte {WAIT_INTERVAL}s...")
|
||||
time.sleep(WAIT_INTERVAL)
|
||||
elapsed += WAIT_INTERVAL
|
||||
return elapsed < MAX_WAIT
|
||||
|
||||
@setup_blueprint.route('/api/status')
|
||||
def status():
|
||||
if not is_configured():
|
||||
return jsonify({"status": "init", "db_connected": False})
|
||||
elif test_connection(load_config()):
|
||||
return jsonify({"status": "ready", "db_connected": True})
|
||||
else:
|
||||
return jsonify({"status": "error", "db_connected": False})
|
||||
|
||||
@setup_blueprint.route('/setup', methods=['GET', 'POST'])
|
||||
def setup():
|
||||
if request.method == 'POST':
|
||||
db_config = {
|
||||
"host": request.form['host'],
|
||||
"port": int(request.form['port']),
|
||||
"user": request.form['user'],
|
||||
"password": request.form['password'],
|
||||
"database": request.form['database']
|
||||
}
|
||||
save_config(db_config)
|
||||
if test_connection(db_config):
|
||||
initialize_admin_user(db_config)
|
||||
return redirect('/')
|
||||
else:
|
||||
return "Verbindung fehlgeschlagen. Bitte zurück und prüfen.", 500
|
||||
return render_template('setup.html')
|
||||
Reference in New Issue
Block a user