Single-command startup and fix bookmarklet
app.py now auto-starts the gateway HTTP server in a daemon thread, so users only need `python app.py` to get everything running. The gateway calls dispatch_request directly when co-located (local mode) instead of trying to establish an RNS link to itself. Bookmarklet hardcoded to localhost:8080. gateway.py still works standalone for connecting to remote instances. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
9a9b5e0617
commit
4e4cc69e0f
3 changed files with 52 additions and 34 deletions
61
gateway.py
61
gateway.py
|
|
@ -16,6 +16,7 @@ class GatewayState:
|
|||
destination = None
|
||||
link = None
|
||||
link_lock = threading.Lock()
|
||||
local_dispatch = None # set when running inside app.py
|
||||
|
||||
|
||||
def resolve_destination(dest_hash_hex):
|
||||
|
|
@ -83,34 +84,40 @@ class GatewayHandler(BaseHTTPRequestHandler):
|
|||
}
|
||||
|
||||
try:
|
||||
link = ensure_link()
|
||||
receipt = link.request(
|
||||
"/tinyweb",
|
||||
data=request_data,
|
||||
timeout=REQUEST_TIMEOUT,
|
||||
)
|
||||
|
||||
# Wait for the response
|
||||
elapsed = 0
|
||||
done_statuses = (RNS.RequestReceipt.READY, RNS.RequestReceipt.DELIVERED, RNS.RequestReceipt.FAILED)
|
||||
while receipt.get_status() not in done_statuses and elapsed < REQUEST_TIMEOUT:
|
||||
time.sleep(0.1)
|
||||
elapsed += 0.1
|
||||
|
||||
if receipt.get_status() in (RNS.RequestReceipt.READY, RNS.RequestReceipt.DELIVERED):
|
||||
resp = receipt.get_response()
|
||||
self.send_response(resp["status"])
|
||||
self.send_header("Content-Type", resp.get("content_type", "text/html; charset=utf-8"))
|
||||
for k, v in resp.get("headers", {}).items():
|
||||
self.send_header(k, v)
|
||||
self.end_headers()
|
||||
resp_body = resp.get("body", "")
|
||||
if resp_body:
|
||||
self.wfile.write(resp_body.encode() if isinstance(resp_body, str) else resp_body)
|
||||
elif receipt.get_status() == RNS.RequestReceipt.FAILED:
|
||||
self.send_error(504, "Request to TinyWeb server failed")
|
||||
if GatewayState.local_dispatch:
|
||||
resp = GatewayState.local_dispatch(request_data)
|
||||
else:
|
||||
self.send_error(504, "Request to TinyWeb server timed out")
|
||||
link = ensure_link()
|
||||
receipt = link.request(
|
||||
"/tinyweb",
|
||||
data=request_data,
|
||||
timeout=REQUEST_TIMEOUT,
|
||||
)
|
||||
|
||||
# Wait for the response
|
||||
elapsed = 0
|
||||
done_statuses = (RNS.RequestReceipt.READY, RNS.RequestReceipt.DELIVERED, RNS.RequestReceipt.FAILED)
|
||||
while receipt.get_status() not in done_statuses and elapsed < REQUEST_TIMEOUT:
|
||||
time.sleep(0.1)
|
||||
elapsed += 0.1
|
||||
|
||||
if receipt.get_status() in (RNS.RequestReceipt.READY, RNS.RequestReceipt.DELIVERED):
|
||||
resp = receipt.get_response()
|
||||
elif receipt.get_status() == RNS.RequestReceipt.FAILED:
|
||||
self.send_error(504, "Request to TinyWeb server failed")
|
||||
return
|
||||
else:
|
||||
self.send_error(504, "Request to TinyWeb server timed out")
|
||||
return
|
||||
|
||||
self.send_response(resp["status"])
|
||||
self.send_header("Content-Type", resp.get("content_type", "text/html; charset=utf-8"))
|
||||
for k, v in resp.get("headers", {}).items():
|
||||
self.send_header(k, v)
|
||||
self.end_headers()
|
||||
resp_body = resp.get("body", "")
|
||||
if resp_body:
|
||||
self.wfile.write(resp_body.encode() if isinstance(resp_body, str) else resp_body)
|
||||
|
||||
except ConnectionError as e:
|
||||
GatewayState.link = None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue