Files
Python-bindings-for-nodejs/python/router.py
2025-11-18 11:02:39 +01:00

43 lines
1.0 KiB
Python

import time
from controller import Controller
import sys
import json
class Router:
def __init__(self):
self.controller = Controller()
self.controller.send = self.send_stream # inject send method here
def send_response(self, response):
print(json.dumps(response))
sys.stdout.flush()
def send_stream(self, stream_data):
print("__STREAM__" + json.dumps(stream_data))
sys.stdout.flush()
def run(self):
for line in sys.stdin:
if not line.strip():
continue
try:
message = json.loads(line)
method = message.get("method")
params = message.get("params", {})
if not hasattr(self.controller, method):
self.send_response({"error": f"Method '{method}' not found in ./python/controller.py"})
continue
method_to_call = getattr(self.controller, method)
# Call method WITHOUT stream_func argument,
# controller methods use self.send() internally
result = method_to_call(params)
self.send_response({"result": result})
except Exception as e:
self.send_response({"error": str(e)})