59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
# app.py (Version mit pyngrok)
|
|
from flask import Flask, jsonify, request
|
|
import subprocess
|
|
import sys
|
|
import os
|
|
import logging
|
|
from pyngrok import ngrok, conf
|
|
from config import Config
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
app = Flask(__name__)
|
|
|
|
# SCRIPT MAP bleibt unverändert
|
|
SCRIPT_MAP = {
|
|
"run_duplicate_check": {"script": "duplicate_checker.py", "args": []},
|
|
"run_reclassify_branches": {"script": "brancheneinstufung2.py", "args": ["--mode", "reclassify_branches"]},
|
|
"run_predict_technicians": {"script": "brancheneinstufung2.py", "args": ["--mode", "predict_technicians"]},
|
|
}
|
|
|
|
def setup_ngrok():
|
|
"""Konfiguriert und startet den ngrok-Tunnel."""
|
|
try:
|
|
authtoken = os.environ.get("NGROK_AUTHTOKEN")
|
|
if not authtoken:
|
|
if os.path.exists("ngrok_authtoken.txt"):
|
|
with open("ngrok_authtoken.txt", "r") as f: authtoken = f.read().strip()
|
|
|
|
if not authtoken:
|
|
logging.error("NGROK_AUTHTOKEN nicht gefunden. Tunnel kann nicht gestartet werden.")
|
|
return None
|
|
|
|
conf.get_default().auth_token = authtoken
|
|
# Starte den Tunnel zum Port, auf dem Flask laufen wird (8080)
|
|
public_url = ngrok.connect(8080, "http")
|
|
logging.info(f"!!! Ngrok-Tunnel gestartet: {public_url} !!!")
|
|
logging.info("!!! Bitte diese URL im Google Apps Script eintragen (falls sie sich geändert hat). !!!")
|
|
return public_url
|
|
except Exception as e:
|
|
logging.error(f"Fehler beim Starten von ngrok: {e}")
|
|
# Beende das Programm, wenn ngrok nicht starten kann
|
|
sys.exit(1)
|
|
|
|
@app.route('/run-script', methods=['POST'])
|
|
def run_script():
|
|
# ... (Ihre run_script Logik bleibt unverändert) ...
|
|
data = request.get_json()
|
|
action = data.get('action')
|
|
if not action or action not in SCRIPT_MAP:
|
|
return jsonify({"status": "error", "message": "Ungültige Aktion."}), 400
|
|
script_config = SCRIPT_MAP[action]
|
|
command = [sys.executable, script_config["script"]] + script_config["args"]
|
|
subprocess.Popen(command)
|
|
return jsonify({"status": "success", "message": f"Aktion '{action}' gestartet."}), 200
|
|
|
|
if __name__ == '__main__':
|
|
# Starte zuerst ngrok
|
|
setup_ngrok()
|
|
# Starte dann den Flask-Server
|
|
app.run(host='0.0.0.0', port=8080) |