enhancement
This commit is contained in:
52
ui/app.py
52
ui/app.py
@@ -1,25 +1,61 @@
|
||||
from flask import Flask, render_template_string
|
||||
from flask import Flask, render_template_string, jsonify
|
||||
from flask_cors import CORS
|
||||
from cloudflare import get_dns_records
|
||||
import os
|
||||
import logging
|
||||
|
||||
# Configurazione logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app) # Abilita CORS per richieste AJAX
|
||||
|
||||
# ----------------------------
|
||||
# Template HTML già esistente
|
||||
# Template HTML
|
||||
# ----------------------------
|
||||
TEMPLATE = open("templates/index.html").read() # Se preferisci usare file separato
|
||||
with open("templates/index.html", encoding="utf-8") as f:
|
||||
TEMPLATE = f.read()
|
||||
|
||||
REFRESH_SECONDS = int(os.getenv("REFRESH_SECONDS", "60"))
|
||||
|
||||
# ----------------------------
|
||||
# Endpoint principale
|
||||
# ----------------------------
|
||||
@app.route("/")
|
||||
def index():
|
||||
records = get_dns_records()
|
||||
|
||||
# Passiamo i dati al template
|
||||
return render_template_string(TEMPLATE, records=records)
|
||||
try:
|
||||
records = get_dns_records()
|
||||
return render_template_string(TEMPLATE, records=records, refresh_seconds=REFRESH_SECONDS)
|
||||
except Exception as e:
|
||||
logger.error(f"Errore nel caricamento dei record: {e}")
|
||||
return render_template_string(TEMPLATE, records=[], refresh_seconds=REFRESH_SECONDS)
|
||||
|
||||
# ----------------------------
|
||||
# API endpoint per refresh AJAX
|
||||
# ----------------------------
|
||||
@app.route("/api/records")
|
||||
def api_records():
|
||||
try:
|
||||
records = get_dns_records()
|
||||
return jsonify({"success": True, "records": records})
|
||||
except Exception as e:
|
||||
logger.error(f"Errore API: {e}")
|
||||
return jsonify({"success": False, "error": str(e)}), 500
|
||||
|
||||
# ----------------------------
|
||||
# Health check
|
||||
# ----------------------------
|
||||
@app.route("/health")
|
||||
def health():
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
# ----------------------------
|
||||
# Avvio Flask
|
||||
# ----------------------------
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000)
|
||||
logger.info(f"Avvio server su porta 5000 con refresh ogni {REFRESH_SECONDS}s")
|
||||
app.run(host="0.0.0.0", port=5000, debug=False)
|
||||
|
||||
Reference in New Issue
Block a user