Replace HTTP server with Reticulum-native architecture. The server now speaks only Reticulum, with a client-side gateway providing browser access by translating HTTP to/from RNS requests. - Extract db layer (db.py), templates (templates.py), handlers (handlers.py) - app.py is now the RNS server with persistent identity and destination - gateway.py bridges HTTP on localhost:8080 to RNS link requests - Add rns dependency, add .gitignore
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
import os
|
|
import time
|
|
import RNS
|
|
|
|
from db import init_db
|
|
from handlers import dispatch_request
|
|
|
|
APP_NAME = "tinyweb"
|
|
ASPECTS = ["server"]
|
|
IDENTITY_FILE = "tinyweb_identity"
|
|
|
|
|
|
def load_or_create_identity():
|
|
if os.path.isfile(IDENTITY_FILE):
|
|
return RNS.Identity.from_file(IDENTITY_FILE)
|
|
identity = RNS.Identity()
|
|
identity.to_file(IDENTITY_FILE)
|
|
return identity
|
|
|
|
|
|
def rns_request_handler(path, data, request_id, link_id, remote_identity, requested_at):
|
|
if data is None:
|
|
data = {"method": "GET", "path": "/", "query": {}, "body": {}, "gateway_host": ""}
|
|
return dispatch_request(data)
|
|
|
|
|
|
def main():
|
|
init_db()
|
|
reticulum = RNS.Reticulum()
|
|
identity = load_or_create_identity()
|
|
|
|
destination = RNS.Destination(
|
|
identity,
|
|
RNS.Destination.IN,
|
|
RNS.Destination.SINGLE,
|
|
APP_NAME,
|
|
*ASPECTS,
|
|
)
|
|
|
|
destination.register_request_handler(
|
|
"/tinyweb",
|
|
response_generator=rns_request_handler,
|
|
allow=RNS.Destination.ALLOW_ALL,
|
|
)
|
|
|
|
destination.announce()
|
|
|
|
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")
|
|
|
|
while True:
|
|
time.sleep(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|