File size: 1,051 Bytes
a4844d1
 
fa0b381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4844d1
 
 
 
 
 
 
fa0b381
a4844d1
 
fa0b381
46e28f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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"])  # 🔥 IMPORTANT DEBUG

    return jsonify({"status": "ok"})
    

app.run(host="0.0.0.0", port=7860)