single-command startup

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.
This commit is contained in:
lichenblankie 2026-03-25 23:01:54 -07:00
parent 7ccaf93404
commit c5d8d350a6
3 changed files with 52 additions and 34 deletions

View file

@ -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