modified: app.py
modified: requirements.txt new file: templates/logs.html
This commit is contained in:
39
app.py
39
app.py
@@ -1,12 +1,20 @@
|
|||||||
from flask import Flask, render_template, redirect, url_for, request, session
|
from flask import Flask, render_template, redirect, url_for, request, session, send_file
|
||||||
|
from flask_socketio import SocketIO, emit
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import psutil
|
import psutil
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
import threading
|
||||||
|
import tailer
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key")
|
app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key")
|
||||||
|
|
||||||
|
socketio = SocketIO(app)
|
||||||
|
|
||||||
|
# Logs Directory
|
||||||
|
LOGS_DIR = "logs"
|
||||||
|
|
||||||
# Verwende Umgebungsvariablen aus Coolify für die Datenbankverbindung
|
# Verwende Umgebungsvariablen aus Coolify für die Datenbankverbindung
|
||||||
DB_HOST = os.getenv("DB_HOST")
|
DB_HOST = os.getenv("DB_HOST")
|
||||||
DB_PORT = os.getenv("DB_PORT")
|
DB_PORT = os.getenv("DB_PORT")
|
||||||
@@ -138,5 +146,34 @@ def users():
|
|||||||
return render_template("users.html", users=users)
|
return render_template("users.html", users=users)
|
||||||
return redirect(url_for("login"))
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
@app.route("/logs")
|
||||||
|
def logs():
|
||||||
|
if "username" in session:
|
||||||
|
log_files = sorted(os.listdir(LOGS_DIR), reverse=True)
|
||||||
|
return render_template("logs.html", log_files=log_files)
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
@app.route("/logs/<log_file>")
|
||||||
|
def view_log(log_file):
|
||||||
|
if "username" in session:
|
||||||
|
log_path = os.path.join(LOGS_DIR, log_file)
|
||||||
|
if os.path.exists(log_path):
|
||||||
|
return send_file(log_path)
|
||||||
|
else:
|
||||||
|
return "Log file not found", 404
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
@socketio.on('connect')
|
||||||
|
def handle_connect():
|
||||||
|
"""Streamt Echtzeit-Logs an den Client."""
|
||||||
|
def tail_log():
|
||||||
|
if bot_status():
|
||||||
|
log_file = os.path.join(LOGS_DIR, f"{datetime.now().strftime('%Y-%m-%d')}.log")
|
||||||
|
for line in tailer.follow(open(log_file)):
|
||||||
|
socketio.emit('log', {'data': line})
|
||||||
|
|
||||||
|
thread = threading.Thread(target=tail_log)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ beautifulsoup4
|
|||||||
pdfplumber
|
pdfplumber
|
||||||
python-dotenv
|
python-dotenv
|
||||||
flask
|
flask
|
||||||
psutil
|
psutil
|
||||||
|
flask-socketio
|
||||||
38
templates/logs.html
Normal file
38
templates/logs.html
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Bot Logs</title>
|
||||||
|
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.3/socket.io.min.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container mt-5">
|
||||||
|
<h1 class="text-center">Bot Logs</h1>
|
||||||
|
<div class="mt-4">
|
||||||
|
<h3>Live Logs</h3>
|
||||||
|
<pre id="live-logs" class="bg-dark text-light p-3" style="height: 300px; overflow-y: scroll;"></pre>
|
||||||
|
</div>
|
||||||
|
<div class="mt-4">
|
||||||
|
<h3>Log Files</h3>
|
||||||
|
<ul class="list-group">
|
||||||
|
{% for log_file in log_files %}
|
||||||
|
<li class="list-group-item">
|
||||||
|
<a href="{{ url_for('view_log', log_file=log_file) }}">{{ log_file }}</a>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<a href="{{ url_for('index') }}" class="btn btn-secondary btn-block mt-3">Back to Dashboard</a>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
var socket = io();
|
||||||
|
socket.on('log', function(msg) {
|
||||||
|
var logs = document.getElementById('live-logs');
|
||||||
|
logs.innerHTML += msg.data + '\n';
|
||||||
|
logs.scrollTop = logs.scrollHeight;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user