Add irr_api.py
This commit is contained in:
parent
8619d990bc
commit
7e4830eb39
1 changed files with 85 additions and 0 deletions
85
irr_api.py
Normal file
85
irr_api.py
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
from flask import Flask, request, jsonify, abort
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
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.")
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def require_api_key(f):
|
||||||
|
@wraps(f)
|
||||||
|
def decorated_function(*args, **kwargs):
|
||||||
|
if request.headers.get('X-API-Key') != EXPECTED_API_KEY:
|
||||||
|
abort(401, description="Invalid API Key")
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
return decorated_function
|
||||||
|
|
||||||
|
@app.route("/ping")
|
||||||
|
def ping():
|
||||||
|
return "pong"
|
||||||
|
|
||||||
|
@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
|
||||||
|
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
|
||||||
|
try:
|
||||||
|
with open(ASN_FILE_PATH, 'w') as f:
|
||||||
|
f.write(content)
|
||||||
|
return jsonify({"success": True, "message": "asns.txt succesvol bijgewerkt."})
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route('/run-update', methods=['POST'])
|
||||||
|
@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."
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route('/log', methods=['GET'])
|
||||||
|
@require_api_key
|
||||||
|
def get_log():
|
||||||
|
"""Haalt de inhoud van het logbestand op."""
|
||||||
|
try:
|
||||||
|
with open(LOG_FILE_PATH, 'r') as f:
|
||||||
|
log_content = f.read()
|
||||||
|
return jsonify({"log": log_content})
|
||||||
|
except FileNotFoundError:
|
||||||
|
return jsonify({"log": "Logbestand nog niet aangemaakt voor deze run."})
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=5001)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue