Skip to content

Commit

Permalink
Better sub-application handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Oct 23, 2024
1 parent 5b38403 commit 941ad68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion runtimepy/data/server_base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ config:
"/index.html": "/app.html"

# Serve these applications by default at these paths.
http_app_paths: ["/", "/app.html", "/index.html"]
http_app_paths: ["/app.html"]

# Handles config["http_app_prefixes"].
config_builders:
- runtimepy.net.html.arbiter.web_app_paths

servers:
- factory: runtimepy_http
Expand Down
32 changes: 32 additions & 0 deletions runtimepy/net/html/arbiter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
A module implementing connection-arbiter related utilities.
"""

# internal
from runtimepy.net.arbiter.config import ConfigObject


def web_app_paths(config: ConfigObject) -> None:
"""
Register boilerplate path handling for additional application-serving URIs.
"""

config.setdefault("config", {})
redirects = config["config"].setdefault("http_redirects", {})
app_paths = config["config"].setdefault("http_app_paths", [])

for prefix in config["config"].get("http_app_prefixes", []):
if not prefix.startswith("/"):
prefix = "/" + prefix

assert not prefix.endswith("/"), prefix

# Add re-directs.
index_path = f"{prefix}/index.html"
app_path = f"{prefix}/app.html"
redirects.setdefault(prefix, index_path)
redirects.setdefault(index_path, app_path)

# Add app path.
if app_path not in app_paths:
app_paths.append(app_path)
2 changes: 2 additions & 0 deletions tests/data/valid/connection_arbiter/runtimepy_http.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ config:
foo: bar
xdg_fragment: "wave1,hide-tabs,hide-channels/wave1:sin,cos"

http_app_prefixes: [rando_app_prefix]

ports:
- {name: tftp_server, type: udp}

Expand Down
4 changes: 3 additions & 1 deletion tests/net/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ async def runtimepy_http_query_peer(app: AppInfo) -> None:

# Try to find peer's HTTP server.
if "proc1" in app.peers:
for port in app.peers["proc1"].peer_config["ports"]: # type: ignore
peer = app.peers["proc1"]
await peer.peer_config_event.wait()
for port in peer.peer_config["ports"]: # type: ignore
if port["name"] == "runtimepy_http_server": # type: ignore
port = port["port"] # type: ignore
break
Expand Down

0 comments on commit 941ad68

Please sign in to comment.