modified: app.py
modified: templates/logs.html
This commit is contained in:
31
app.py
31
app.py
@@ -1,12 +1,15 @@
|
||||
from flask import Flask, render_template, redirect, url_for, request, session
|
||||
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_file
|
||||
import os
|
||||
import subprocess
|
||||
import psutil
|
||||
import mysql.connector
|
||||
from datetime import datetime
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key")
|
||||
|
||||
LOG_FILE_PATH = os.path.join("logs", f"{datetime.now().strftime('%Y-%m-%d')}.log")
|
||||
|
||||
# Verwende Umgebungsvariablen aus Coolify für die Datenbankverbindung
|
||||
DB_HOST = os.getenv("DB_HOST")
|
||||
DB_PORT = os.getenv("DB_PORT")
|
||||
@@ -138,5 +141,31 @@ def users():
|
||||
return render_template("users.html", users=users)
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/logs")
|
||||
def view_logs():
|
||||
"""Zeigt die Logs des Bots im Admin-Panel an."""
|
||||
if "username" in session:
|
||||
return render_template("logs.html")
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/get_logs")
|
||||
def get_logs():
|
||||
"""Liest den Inhalt der Log-Datei und gibt ihn zurück."""
|
||||
if "username" in session:
|
||||
try:
|
||||
with open(LOG_FILE_PATH, 'r', encoding='utf-8') as file:
|
||||
logs = file.read()
|
||||
return jsonify({"logs": logs})
|
||||
except FileNotFoundError:
|
||||
return jsonify({"logs": "Log file not found."})
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/download_logs")
|
||||
def download_logs():
|
||||
"""Bietet die Log-Datei zum Download an."""
|
||||
if "username" in session:
|
||||
return send_file(LOG_FILE_PATH, as_attachment=True)
|
||||
return redirect(url_for("login"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
|
||||
@@ -5,34 +5,31 @@
|
||||
<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 class="card mt-4">
|
||||
<div class="card-body">
|
||||
<button class="btn btn-primary mb-3" onclick="refreshLogs()">Refresh Logs</button>
|
||||
<a href="{{ url_for('download_logs') }}" class="btn btn-secondary mb-3">Download Logs</a>
|
||||
<pre id="log-content" style="height: 500px; overflow-y: scroll; background-color: #f8f9fa; padding: 10px; border: 1px solid #ddd;"></pre>
|
||||
<a href="{{ url_for('index') }}" class="btn btn-secondary btn-block mt-3">Back to Dashboard</a>
|
||||
</div>
|
||||
</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 src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
var socket = io();
|
||||
socket.on('log', function(msg) {
|
||||
var logs = document.getElementById('live-logs');
|
||||
logs.innerHTML += msg.data + '\n';
|
||||
logs.scrollTop = logs.scrollHeight;
|
||||
});
|
||||
function refreshLogs() {
|
||||
fetch('{{ url_for("get_logs") }}')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
document.getElementById("log-content").textContent = data.logs;
|
||||
});
|
||||
}
|
||||
|
||||
window.onload = refreshLogs;
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user