from flask import Flask, render_template, render_template_string import configparser import ssl 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 from auth.auth_middleware import setup_auth from log_parser import parse_log_file app = Flask(__name__) # Load basic auth credentials auth_config = configparser.ConfigParser() auth_config.read('/etc/haproxy-configurator/auth/auth.cfg') BASIC_AUTH_USERNAME = auth_config.get('auth', 'username') BASIC_AUTH_PASSWORD = auth_config.get('auth', 'password') # Register blueprints app.register_blueprint(main_bp) app.register_blueprint(edit_bp) # Setup authentication (placeholder, not currently used) setup_auth(app) # SSL Configuration config2 = configparser.ConfigParser() config2.read('/etc/haproxy-configurator/ssl.ini') certificate_path = config2.get('ssl', 'certificate_path') private_key_path = config2.get('ssl', 'private_key_path') ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) ssl_context.load_cert_chain(certfile=certificate_path, keyfile=private_key_path) # Statistics Route @app.route('/statistics') def display_haproxy_stats(): haproxy_stats = fetch_haproxy_stats() parsed_stats = parse_haproxy_stats(haproxy_stats) return render_template_string('''
Home Add Frontend&Backend Edit HAProxy Config Security Events Statistics HAProxy Stats

HAProxy Stats

{% for stat in stats %} {% endfor %}
Frontend Name Server Name 4xx Errors 5xx Errors Bytes In (MB) Bytes Out (MB) Total Connections
{{ stat.frontend_name }} {{ stat.server_name }} {{ stat['4xx_errors'] }} {{ stat['5xx_errors'] }} {{ stat.bytes_in_mb }} {{ stat.bytes_out_mb }} {{ stat.conn_tot }}
''', stats=parsed_stats) # Logs Route @app.route('/logs') def display_logs(): log_file_path = '/var/log/haproxy.log' parsed_entries = parse_log_file(log_file_path) return render_template('logs.html', entries=parsed_entries) if __name__ == '__main__': app.run(host='::', port=5000, ssl_context=ssl_context, debug=True)