| from flask import Flask, request, jsonify |
| from flask import Response |
| import time |
|
|
| app = Flask(__name__) |
|
|
| logs = [] |
|
|
| @app.route("/") |
| def index(): |
| return """ |
| <h2>Live Logs</h2> |
| <pre id="logs"></pre> |
| |
| <script> |
| var evtSource = new EventSource("/stream"); |
| evtSource.onmessage = function(e) { |
| document.getElementById("logs").textContent += e.data + "\\n"; |
| }; |
| </script> |
| """ |
|
|
| @app.route("/stream") |
| def stream(): |
| def event_stream(): |
| last = 0 |
| while True: |
| if len(logs) > last: |
| yield f"data: {logs[-1]}\\n\\n" |
| last = len(logs) |
| time.sleep(0.5) |
| return Response(event_stream(), mimetype="text/event-stream") |
|
|
|
|
| @app.route("/log", methods=["POST"]) |
| def log(): |
| data = request.json |
|
|
| if not data or "log" not in data: |
| return jsonify({"error": "no log"}), 400 |
|
|
| logs.append(data["log"]) |
| print("LOG RECEIVED:", data["log"]) |
|
|
| return jsonify({"status": "ok"}) |
| |
|
|
| app.run(host="0.0.0.0", port=7860) |