Spaces:
Runtime error

File size: 2,599 Bytes
d516e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Local error sink. Accepts the fusion webhook and a Sentry-style envelope.

Events stay on disk. Nothing is forwarded.
"""

from __future__ import annotations

import json
import os
import sys
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path

LOG = Path(os.environ.get("AUREON_ERROR_SINK_PATH", "/tmp/aureon-errors.jsonl"))


class Handler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"

    def do_GET(self) -> None:  # noqa: N802
        path = self.path.split("?", 1)[0]
        if path in {"/", "/health", "/healthz", "/status"}:
            self._send(200, {"status": "ok", "live_trading": False, "may_act": False})
            return
        if path == "/events":
            count = 0
            if LOG.is_file():
                count = sum(1 for line in LOG.read_text(encoding="utf-8").splitlines() if line.strip())
            self._send(200, {"count": count, "live_trading": False})
            return
        self._send(404, {"error": "not_found", "live_trading": False})

    def do_POST(self) -> None:  # noqa: N802
        length = int(self.headers.get("Content-Length", "0") or "0")
        raw = self.rfile.read(min(length, 1_000_000))
        try:
            payload = json.loads(raw.decode("utf-8"))
            if not isinstance(payload, dict):
                payload = {"body": payload}
        except (UnicodeError, json.JSONDecodeError):
            payload = {"raw_bytes": len(raw)}
        payload["sink_path"] = self.path.split("?", 1)[0]
        payload["live_trading"] = False
        LOG.parent.mkdir(parents=True, exist_ok=True)
        with LOG.open("a", encoding="utf-8") as handle:
            handle.write(json.dumps(payload, sort_keys=True, default=str) + "\n")
        self._send(200, {"ok": True, "live_trading": False})

    def log_message(self, fmt: str, *args) -> None:
        return

    def _send(self, status: int, payload: dict) -> None:
        body = json.dumps(payload, sort_keys=True).encode("utf-8")
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)


def main() -> int:
    host = os.environ.get("AUREON_ERROR_SINK_BIND", "0.0.0.0")
    port = int(os.environ.get("AUREON_ERROR_SINK_PORT", "8088"))
    server = ThreadingHTTPServer((host, port), Handler)
    print(f"error sink http://{host}:{port}/  live_trading=false", flush=True)
    server.serve_forever()
    return 0


if __name__ == "__main__":
    sys.exit(main())