diff --git a/app.py b/app.py index 890bf0a..9233911 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,12 @@ import os import time +import threading import RNS +from http.server import HTTPServer from db import init_db from handlers import dispatch_request +from gateway import GatewayState, GatewayHandler, GATEWAY_PORT APP_NAME = "tinyweb" ASPECTS = ["server"] @@ -24,6 +27,14 @@ def rns_request_handler(path, data, request_id, link_id, remote_identity, reques return dispatch_request(data) +def start_gateway(reticulum): + GatewayState.reticulum = reticulum + GatewayState.local_dispatch = dispatch_request + server = HTTPServer(("127.0.0.1", GATEWAY_PORT), GatewayHandler) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + + def main(): init_db() reticulum = RNS.Reticulum() @@ -44,10 +55,11 @@ def main(): ) destination.announce() + start_gateway(reticulum) - print(f"TinyWeb Reticulum server running") - print(f"Destination hash: {RNS.prettyhexrep(destination.hash)}") - print(f"Share this hash with clients to connect via gateway.py") + print(f"TinyWeb running!") + print(f"Open http://localhost:{GATEWAY_PORT} in your browser") + print(f"Destination hash: {RNS.prettyhexrep(destination.hash)} (share this so friends can subscribe)") while True: time.sleep(1) diff --git a/gateway.py b/gateway.py index 0bde2d3..78516b3 100644 --- a/gateway.py +++ b/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 diff --git a/handlers.py b/handlers.py index ec5f6dd..4550e15 100644 --- a/handlers.py +++ b/handlers.py @@ -308,12 +308,11 @@ def handle_import_submit(body): return handle_import_form(f"Imported {imported} page(s). {errors} error(s).") -def handle_style_form(msg="", gateway_host=""): +def handle_style_form(msg=""): css = get_setting("custom_css") name = get_site_name() sharing = get_setting("sharing_enabled", "0") checked = " checked" if sharing == "1" else "" - host = gateway_host or "localhost:8080" return _respond( f"
Drag this link to your bookmarks bar. Click it on any page to index it instantly.
" - f'' + f'' f"{msg}
" f'back' ) @@ -651,7 +650,7 @@ def dispatch_request(data): elif path == "/bookmark": return handle_bookmark(query) elif path == "/style": - return handle_style_form(gateway_host=gateway_host) + return handle_style_form() elif path == "/export": return handle_export() elif path == "/import":