supervisord
This commit is contained in:
82
app.py
82
app.py
@@ -1,3 +1,4 @@
|
||||
|
||||
from flask import Flask, render_template, render_template_string
|
||||
import configparser
|
||||
import ssl
|
||||
@@ -6,10 +7,38 @@ 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
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Uniwersalne ścieżki - sprawdzaj obie
|
||||
CONFIG_DIR_DOCKER = '/etc/haproxy-configurator'
|
||||
CONFIG_DIR_LOCAL = './config'
|
||||
CONFIG_DIR_ENV = os.environ.get('CONFIG_DIR', None)
|
||||
|
||||
# Określ która ścieżka istnieje
|
||||
if CONFIG_DIR_ENV and os.path.exists(CONFIG_DIR_ENV):
|
||||
CONFIG_DIR = CONFIG_DIR_ENV
|
||||
elif os.path.exists(CONFIG_DIR_DOCKER):
|
||||
CONFIG_DIR = CONFIG_DIR_DOCKER
|
||||
elif os.path.exists(CONFIG_DIR_LOCAL):
|
||||
CONFIG_DIR = CONFIG_DIR_LOCAL
|
||||
else:
|
||||
CONFIG_DIR = CONFIG_DIR_DOCKER # Fallback
|
||||
|
||||
AUTH_CFG = os.path.join(CONFIG_DIR, 'auth', 'auth.cfg')
|
||||
SSL_INI = os.path.join(CONFIG_DIR, 'ssl.ini')
|
||||
|
||||
# Create directories
|
||||
os.makedirs(os.path.dirname(AUTH_CFG), exist_ok=True)
|
||||
os.makedirs(os.path.dirname(SSL_INI), exist_ok=True)
|
||||
|
||||
# Load basic auth credentials
|
||||
BASIC_AUTH_USERNAME = "admin"
|
||||
BASIC_AUTH_PASSWORD = "admin"
|
||||
|
||||
try:
|
||||
auth_config = configparser.ConfigParser()
|
||||
auth_config.read(AUTH_CFG)
|
||||
@@ -24,20 +53,50 @@ except Exception as e:
|
||||
BASIC_AUTH_USERNAME = "admin"
|
||||
BASIC_AUTH_PASSWORD = "admin"
|
||||
|
||||
|
||||
# Register blueprints
|
||||
app.register_blueprint(main_bp)
|
||||
app.register_blueprint(edit_bp)
|
||||
|
||||
# Setup authentication (placeholder, not currently used)
|
||||
|
||||
# Setup authentication
|
||||
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)
|
||||
|
||||
# SSL Configuration - Z ERROR HANDLINGIEM
|
||||
certificate_path = None
|
||||
private_key_path = None
|
||||
ssl_context = None
|
||||
|
||||
try:
|
||||
config2 = configparser.ConfigParser()
|
||||
config2.read(SSL_INI)
|
||||
|
||||
# WAŻNE: has_section check PRZED .get()
|
||||
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)
|
||||
|
||||
# Sprawdź czy pliki istnieją
|
||||
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)
|
||||
|
||||
|
||||
# Statistics Route
|
||||
@app.route('/statistics')
|
||||
@@ -55,11 +114,13 @@ def display_haproxy_stats():
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
|
||||
.logo {
|
||||
width: 300px; /* Adjust the width as needed */
|
||||
height: auto;
|
||||
}
|
||||
|
||||
|
||||
.menu-link {
|
||||
text-decoration: none;
|
||||
padding: 10px 20px;
|
||||
@@ -67,6 +128,7 @@ def display_haproxy_stats():
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
|
||||
.menu-link:hover {
|
||||
background-color: #3B444B;
|
||||
color: white;
|
||||
@@ -120,6 +182,7 @@ def display_haproxy_stats():
|
||||
</div>
|
||||
''', stats=parsed_stats)
|
||||
|
||||
|
||||
# Logs Route
|
||||
@app.route('/logs')
|
||||
def display_logs():
|
||||
@@ -127,5 +190,6 @@ def display_logs():
|
||||
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)
|
||||
app.run(host='::', port=5000, ssl_context=ssl_context, debug=True)
|
||||
Reference in New Issue
Block a user