File size: 1,618 Bytes
415196f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Minimal mock backend that accepts POST /update and POST /finish and prints the body.
Run this, then set SYMBIOS_BACKEND_BASE_URL=http://localhost:9999 and trigger
a vectorize job to see progress and finish callbacks.

Usage:
  python scripts/mock_backend.py
  # or: .upload_venv/bin/python scripts/mock_backend.py
"""
import json
from http.server import HTTPServer, BaseHTTPRequestHandler

PORT = 9999


class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        length = int(self.headers.get("Content-Length", 0))
        body = self.rfile.read(length) if length else b""
        try:
            data = json.loads(body.decode()) if body else {}
        except Exception:
            data = body.decode(errors="replace")
        auth = self.headers.get("Authorization", "(none)")
        print(f"[{self.command} {self.path}] Authorization: {auth}")
        print(json.dumps(data, indent=2))
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(b'{"ok": true}\n')

    def log_message(self, format, *args):
        pass  # suppress default request log; we print above


def main():
    server = HTTPServer(("", PORT), Handler)
    print(f"Mock backend listening on http://localhost:{PORT}")
    print("  POST /update  - progress (doc_id, finished, total, message)")
    print("  POST /finish  - completion (status, vec_id, ...)")
    print("Set SYMBIOS_BACKEND_BASE_URL=http://localhost:9999 and run a vectorize job.\n")
    server.serve_forever()


if __name__ == "__main__":
    main()