157 lines
4.9 KiB
Python
157 lines
4.9 KiB
Python
from flask import Flask, render_template, redirect, url_for, request, session
|
|
import os
|
|
import subprocess
|
|
import mysql.connector
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key")
|
|
|
|
# Verwende Umgebungsvariablen aus Coolify für die Datenbankverbindung
|
|
DB_HOST = os.getenv("DB_HOST")
|
|
DB_PORT = os.getenv("DB_PORT")
|
|
DB_USER = os.getenv("DB_USER")
|
|
DB_PASS = os.getenv("DB_PASS")
|
|
DB_NAME = os.getenv("DB_NAME")
|
|
|
|
# Globale Variablen für die Intros und Themes
|
|
INTRO_FILE = "introduction.txt"
|
|
ASKNOTES_INTRO_FILE = "asknotesintro.txt"
|
|
THEME_FILE = "theme.txt"
|
|
|
|
# Speichern der Prozess-ID
|
|
bot_process = None
|
|
|
|
def bot_status():
|
|
"""Überprüft, ob der Bot läuft."""
|
|
global bot_process
|
|
if bot_process is None:
|
|
return False
|
|
return bot_process.poll() is None # None bedeutet, dass der Prozess noch läuft
|
|
|
|
def start_bot():
|
|
"""Startet den Bot."""
|
|
global bot_process
|
|
if not bot_status():
|
|
bot_process = subprocess.Popen(["python", "bot.py"], cwd=os.path.dirname(os.path.abspath(__file__)))
|
|
else:
|
|
print("Bot läuft bereits.")
|
|
|
|
def stop_bot():
|
|
"""Stoppt den Bot."""
|
|
global bot_process
|
|
if bot_process and bot_status():
|
|
bot_process.terminate()
|
|
bot_process.wait() # Warten, bis der Prozess beendet ist
|
|
bot_process = None
|
|
else:
|
|
print("Bot läuft nicht.")
|
|
|
|
def load_text_file(file_path):
|
|
"""Lädt den Inhalt einer Textdatei."""
|
|
if os.path.exists(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
return file.read()
|
|
return ""
|
|
|
|
def save_text_file(file_path, content):
|
|
"""Speichert den Inhalt in einer Textdatei."""
|
|
with open(file_path, 'w', encoding='utf-8') as file:
|
|
file.write(content)
|
|
|
|
def get_db_connection():
|
|
"""Stellt eine Verbindung zur MySQL-Datenbank her."""
|
|
return mysql.connector.connect(
|
|
host=DB_HOST,
|
|
port=DB_PORT,
|
|
user=DB_USER,
|
|
password=DB_PASS,
|
|
database=DB_NAME
|
|
)
|
|
|
|
def get_current_theme():
|
|
"""Lädt das aktuelle Theme."""
|
|
return load_text_file(THEME_FILE) or "light" # Standard-Theme ist 'light'
|
|
|
|
def set_current_theme(theme):
|
|
"""Speichert das aktuelle Theme."""
|
|
save_text_file(THEME_FILE, theme)
|
|
|
|
@app.route("/")
|
|
def index():
|
|
if "username" in session:
|
|
theme = get_current_theme()
|
|
return render_template("index.html", bot_running=bot_status(), theme=theme)
|
|
return redirect(url_for("login"))
|
|
|
|
@app.route("/login", methods=["GET", "POST"])
|
|
def login():
|
|
if request.method == "POST":
|
|
username = request.form["username"]
|
|
password = request.form["password"]
|
|
if username == os.getenv("ADMIN_USER") and password == os.getenv("ADMIN_PASS"):
|
|
session["username"] = username
|
|
return redirect(url_for("index"))
|
|
else:
|
|
return "Invalid credentials!"
|
|
theme = get_current_theme()
|
|
return render_template("login.html", theme=theme)
|
|
|
|
@app.route("/logout")
|
|
def logout():
|
|
session.pop("username", None)
|
|
return redirect(url_for("login"))
|
|
|
|
@app.route("/start_bot")
|
|
def start():
|
|
if "username" in session:
|
|
start_bot()
|
|
return redirect(url_for("index"))
|
|
return redirect(url_for("login"))
|
|
|
|
@app.route("/stop_bot")
|
|
def stop():
|
|
if "username" in session:
|
|
stop_bot()
|
|
return redirect(url_for("index"))
|
|
return redirect(url_for("login"))
|
|
|
|
@app.route("/settings", methods=["GET", "POST"])
|
|
def settings():
|
|
if "username" in session:
|
|
if request.method == "POST":
|
|
introduction = request.form.get("introduction")
|
|
asknotes_introduction = request.form.get("asknotes_introduction")
|
|
theme = request.form.get("theme")
|
|
|
|
# Speichern der Intros und des Themes
|
|
save_text_file(INTRO_FILE, introduction)
|
|
save_text_file(ASKNOTES_INTRO_FILE, asknotes_introduction)
|
|
set_current_theme(theme)
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
# Laden der aktuellen Inhalte aus den Textdateien
|
|
introduction = load_text_file(INTRO_FILE)
|
|
asknotes_introduction = load_text_file(ASKNOTES_INTRO_FILE)
|
|
current_theme = get_current_theme()
|
|
|
|
return render_template("settings.html", introduction=introduction, asknotes_introduction=asknotes_introduction, current_theme=current_theme)
|
|
return redirect(url_for("login"))
|
|
|
|
@app.route("/users")
|
|
def users():
|
|
"""Zeigt eine Liste aller Benutzer aus der Datenbank an."""
|
|
if "username" in session:
|
|
connection = get_db_connection()
|
|
cursor = connection.cursor(dictionary=True)
|
|
cursor.execute("SELECT * FROM user_data")
|
|
users = cursor.fetchall()
|
|
cursor.close()
|
|
connection.close()
|
|
theme = get_current_theme()
|
|
return render_template("users.html", users=users, theme=theme)
|
|
return redirect(url_for("login"))
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=5000, debug=True)
|