This commit is contained in:
Mateusz Gruszczyński
2025-11-04 08:26:41 +01:00
parent 34c84f1115
commit 27f9984574
2 changed files with 153 additions and 125 deletions

80
app.py
View File

@@ -1,10 +1,8 @@
import os
import sys
import ssl
import configparser
from flask import Flask, render_template, render_template_string
from flask import Flask, render_template, render_template_string, request, jsonify
from routes.main_routes import main_bp
from routes.edit_routes import edit_bp
from utils.stats_utils import fetch_haproxy_stats, parse_haproxy_stats
@@ -59,7 +57,6 @@ except Exception as e:
app.register_blueprint(main_bp)
app.register_blueprint(edit_bp)
setup_auth(app)
certificate_path = None
@@ -69,71 +66,98 @@ ssl_context = None
try:
config2 = configparser.ConfigParser()
config2.read(SSL_INI)
if config2.has_section('ssl'):
certificate_path = config2.get('ssl', 'certificate_path')
private_key_path = config2.get('ssl', 'private_key_path')
else:
print(f"[APP] No [ssl] section in {SSL_INI}", flush=True)
sys.exit(1)
if not os.path.exists(certificate_path):
print(f"[APP] Certificate not found: {certificate_path}", flush=True)
sys.exit(1)
if not os.path.exists(private_key_path):
print(f"[APP] Private key not found: {private_key_path}", flush=True)
sys.exit(1)
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_context.load_cert_chain(certfile=certificate_path, keyfile=private_key_path)
print(f"[APP] SSL context loaded", flush=True)
except Exception as e:
print(f"[APP] SSL error: {e}", flush=True)
sys.exit(1)
@app.route('/statistics')
def display_haproxy_stats():
haproxy_stats = fetch_haproxy_stats()
parsed_stats = parse_haproxy_stats(haproxy_stats)
return render_template('statistics.html', stats=parsed_stats)
@app.route('/logs', endpoint='display_logs')
#@requires_auth
def display_haproxy_logs():
log_file_path = '/var/log/haproxy.log'
if not os.path.exists(log_file_path):
return render_template('logs.html',
logs=[],
return render_template('logs.html',
logs=[],
total_logs=0,
error_message=f"Log file not found: {log_file_path}")
try:
logs = parse_log_file(log_file_path)
if not logs:
return render_template('logs.html',
logs=[],
error_message="Log file is empty or unreadable")
return render_template('logs.html', logs=logs)
except Exception as e:
total_logs = len(logs)
# Załaduj ostatnie 200 logów
initial_logs = logs[-200:] if len(logs) > 200 else logs
return render_template('logs.html',
logs=[],
logs=initial_logs,
total_logs=total_logs,
loaded_count=len(initial_logs))
except Exception as e:
return render_template('logs.html',
logs=[],
total_logs=0,
error_message=f"Error parsing logs: {str(e)}")
@app.route('/api/logs', methods=['POST'])
def api_get_logs():
try:
log_file_path = '/var/log/haproxy.log'
if not os.path.exists(log_file_path):
return jsonify({'error': 'Log file not found'}), 404
page = request.json.get('page', 1)
per_page = request.json.get('per_page', 50)
offset = (page - 1) * per_page
logs = parse_log_file(log_file_path)
total_logs = len(logs)
reversed_logs = logs[::-1]
paginated_logs = reversed_logs[offset:offset + per_page]
return jsonify({
'success': True,
'logs': paginated_logs,
'page': page,
'per_page': per_page,
'total': total_logs,
'has_more': offset + per_page < total_logs
})
except Exception as e:
print(f"[API] Error: {e}", flush=True)
return jsonify({'error': str(e), 'success': False}), 500
@app.route('/home')
def home():
frontend_count, backend_count, acl_count, layer7_count, layer4_count = count_frontends_and_backends()
return render_template('home.html',
frontend_count=frontend_count,
backend_count=backend_count,
return render_template('home.html',
frontend_count=frontend_count,
backend_count=backend_count,
acl_count=acl_count,
layer7_count=layer7_count,
layer7_count=layer7_count,
layer4_count=layer4_count)
if __name__ == '__main__':
app.run(host='::', port=5000, ssl_context=ssl_context, debug=True)
app.run(host='::', port=5000, ssl_context=ssl_context, debug=True)