From 7c9765fd23dbef789191d12fcf8bbe15a8907348 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Tue, 28 Jun 2022 11:13:20 -0500 Subject: [PATCH] http_translator: control over port binding --- http_protocol.prs | 2 ++ http_translator.config-example.pr | 9 ++++----- src/http_translator.nim | 7 +++++-- src/schema/http_protocol.nim | 8 ++++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/http_protocol.prs b/http_protocol.prs index af2e8cf..29c600f 100644 --- a/http_protocol.prs +++ b/http_protocol.prs @@ -6,6 +6,8 @@ Methods = #{Method} . ; A URL path split into elements Path = [string ...] . +Listener = . + ; Register an entity that will handle requests at path prefix. ; TODO: assert the public base URL of the handler to the entity. Handler = . diff --git a/http_translator.config-example.pr b/http_translator.config-example.pr index 09dc41d..13fc302 100644 --- a/http_translator.config-example.pr +++ b/http_translator.config-example.pr @@ -13,9 +13,8 @@ $other [ ] ] -? ?cap> [ - $cap [ - ; publish GET requests with prefix "/foo/bar" to other dataspace - - ] +? ?cap> $cap [ + + ; publish GET requests with prefix "/foo/bar" to other dataspace + handler #{GET} ["foo" "bar" ] $other> ] diff --git a/src/http_translator.nim b/src/http_translator.nim index d50fb82..9574033 100644 --- a/src/http_translator.nim +++ b/src/http_translator.nim @@ -83,7 +83,10 @@ bootDataspace("main") do (ds: Ref; turn: var Turn): run(parentFacet) do (turn: var Turn): retract(turn, rHandle) hitch(fut, parentFut) - var http = newAsyncHttpServer() - asyncCheck serve(http, Port 8888, handleRequest) + during(turn, ds, ?Listener) do (port: Port): + var http = newAsyncHttpServer() + asyncCheck serve(http, port, handleRequest) + do: + close(http) runForever() diff --git a/src/schema/http_protocol.nim b/src/schema/http_protocol.nim index 2f9117b..9c5f7ac 100644 --- a/src/schema/http_protocol.nim +++ b/src/schema/http_protocol.nim @@ -11,6 +11,9 @@ type `headers`*: Headers `body`*: string + Listener* {.preservesRecord: "listen".} = object + `port`*: int + Handler*[E] {.preservesRecord: "handler".} = ref object `methods`*: Methods `path`*: Path @@ -33,8 +36,9 @@ proc `$`*[E](x: Handler[E]): string = proc encode*[E](x: Handler[E]): seq[byte] = encode(toPreserve(x, E)) -proc `$`*(x: Path | Headers | Response | Request | Methods): string = +proc `$`*(x: Path | Headers | Response | Listener | Request | Methods): string = `$`(toPreserve(x)) -proc encode*(x: Path | Headers | Response | Request | Methods): seq[byte] = +proc encode*(x: Path | Headers | Response | Listener | Request | Methods): seq[ + byte] = encode(toPreserve(x))