Update irr_api.py

This commit is contained in:
Blackwhitebear8 2025-10-31 20:57:29 +01:00
parent 242469903a
commit 733cbacfef

View file

@ -7,10 +7,7 @@ ASN_FILE_PATH = os.getenv("ASN_FILE_PATH", "/opt/irr-update/asns.txt")
LOG_FILE_PATH = os.getenv("LOG_FILE_PATH", "/opt/irr-update/irr_updater.log")
SCRIPT_PATH = os.getenv("SCRIPT_PATH", "/opt/irr-update/gen.sh")
EXPECTED_API_KEY = "jouw-zeer-geheime-key-123"
if not EXPECTED_API_KEY:
raise ValueError("EXPECTED_API_KEY is niet ingesteld in het script.")
EXPECTED_API_KEY = "your-secret-key-123"
app = Flask(__name__)
@ -29,27 +26,25 @@ def ping():
@app.route('/asns', methods=['GET'])
@require_api_key
def get_asns():
"""Haalt de huidige inhoud van asns.txt op."""
try:
with open(ASN_FILE_PATH, 'r') as f:
content = f.read()
return jsonify({"content": content})
except FileNotFoundError:
return jsonify({"error": "asns.txt niet gevonden."}), 404
return jsonify({"error": "asns.txt not found."}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/asns', methods=['POST'])
@require_api_key
def update_asns():
"""Werkt de inhoud van asns.txt bij."""
content = request.json.get('content')
if content is None:
return jsonify({"error": "Geen 'content' in request body."}), 400
return jsonify({"error": "Missing 'content' in request body."}), 400
try:
with open(ASN_FILE_PATH, 'w') as f:
f.write(content)
return jsonify({"success": True, "message": "asns.txt succesvol bijgewerkt."})
return jsonify({"success": True, "message": "asns.txt updated successfully."})
except Exception as e:
return jsonify({"error": str(e)}), 500
@ -57,13 +52,10 @@ def update_asns():
@require_api_key
def run_update():
try:
open(LOG_FILE_PATH, 'w').close()
subprocess.Popen([SCRIPT_PATH])
return jsonify({
"success": True,
"message": "IRR update script gestart in de achtergrond. Log wordt nu gevuld."
"message": "IRR update script started in background."
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@ -71,15 +63,38 @@ def run_update():
@app.route('/log', methods=['GET'])
@require_api_key
def get_log():
"""Haalt de inhoud van het logbestand op."""
START_MARKER = "Starting IRR filter update process."
NUM_RUNS_TO_SHOW = 3
try:
with open(LOG_FILE_PATH, 'r') as f:
log_content = f.read()
return jsonify({"log": log_content})
lines = f.readlines()
except FileNotFoundError:
return jsonify({"log": "Logbestand nog niet aangemaakt voor deze run."})
return jsonify({"log": "Log file not found."})
except Exception as e:
return jsonify({"error": str(e)}), 500
if not lines:
return jsonify({"log": "Log file is empty."})
start_indices = [i for i, line in enumerate(lines) if START_MARKER in line]
if not start_indices:
return jsonify({"log": "".join(lines)})
num_runs_found = len(start_indices)
if num_runs_found <= NUM_RUNS_TO_SHOW:
run_start_index = start_indices[0]
else:
run_start_index = start_indices[num_runs_found - NUM_RUNS_TO_SHOW]
final_start_index = max(0, run_start_index - 1)
last_runs_lines = lines[final_start_index:]
log_content = "".join(last_runs_lines)
return jsonify({"log": log_content})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)