62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
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
|
|
# ----------------------------
|
|
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():
|
|
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__":
|
|
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)
|