From fc9b54f3398bc3461389ab3fb3c4db4dbcf1a8c1 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sun, 7 May 2023 09:21:07 +0100 Subject: [PATCH] Remove http_translator There is better HTTP schema defined elsewhere. See syndicate-protocols/schemas/http.prs. --- .gitignore | 1 - README.md | 6 --- http_protocol.prs | 22 -------- http_translator.config-example.pr | 20 ------- src/http_translator.nim | 90 ------------------------------- src/schema/http_protocol.nim | 38 ------------- syndicate_utils.nimble | 2 +- 7 files changed, 1 insertion(+), 178 deletions(-) delete mode 100644 http_protocol.prs delete mode 100644 http_translator.config-example.pr delete mode 100644 src/http_translator.nim delete mode 100644 src/schema/http_protocol.nim diff --git a/.gitignore b/.gitignore index b6be70a..c30fbcc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /.direnv -http_translator json_translator msg json_socket_translator diff --git a/README.md b/README.md index 2f409ca..e29569d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ # Syndicate utils -## http_translator - -Dispatches HTTP requests to registered handlers. - -See [http_translator.config-example.pr](./http_translator.config-example.pr) for an example configuration. - ## json_translator Wrapper that executes a command, parses its JSON output, and asserts a Preserves conversion in an `` record. diff --git a/http_protocol.prs b/http_protocol.prs deleted file mode 100644 index 29c600f..0000000 --- a/http_protocol.prs +++ /dev/null @@ -1,22 +0,0 @@ -version 1 . - -Method = =GET / =HEAD / =POST / =PUT / =DELETE / =CONNECT / =OPTIONS / =TRACE / =PATCH . -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 = . - -Headers = {string: [string ...] ...:...} . - -; A request awaiting a response at handle. -; TODO: query parameters -Request = . - -; A response to handle. -Response = . diff --git a/http_translator.config-example.pr b/http_translator.config-example.pr deleted file mode 100644 index 13fc302..0000000 --- a/http_translator.config-example.pr +++ /dev/null @@ -1,20 +0,0 @@ -> - - - -let ?other = dataspace - -$other [ - ? [ - - ] -] - -? ?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 deleted file mode 100644 index 1ee968c..0000000 --- a/src/http_translator.nim +++ /dev/null @@ -1,90 +0,0 @@ -# SPDX-FileCopyrightText: ☭ Emery Hemingway -# SPDX-License-Identifier: Unlicense - -import std/[asyncdispatch, asynchttpserver, strutils, tables, uri] -import preserves -import syndicate, syndicate/actors - -import ./schema/http_protocol - -func toHttpCore(methods: Methods): set[HttpMethod] = - # Convert the schema type to the type in the httpcore module. - for m in methods: - result.incl( - case m - of Method.GET: HttpGET - of Method.HEAD: HttpHEAD - of Method.POST: HttpPOST - of Method.PUT: HttpPUT - of Method.DELETE: HttpDELETE - of Method.CONNECT: HttpCONNECT - of Method.OPTIONS: HttpOPTIONS - of Method.TRACE: HttpTRACE - of Method.PATCH: HttpPATCH) - -proc splitPath(u: Uri): Path = - u.path.strip(chars = {'/'}).split("/") - -proc hitch(a, b: Future[void]) = - a.addCallback do (f: Future[void]): - if f.failed: fail(b, f.error) - else: complete(b) - -runActor("main") do (ds: Ref; turn: var Turn): - connectStdio(ds, turn) - var - handlers: Table[seq[string], (Ref, set[HttpMethod])] - pathPrefixMaxLen = 0 - requestIdSource = 0 - - during(turn, ds, ?Handler) do (methods: Methods; path: seq[string]; entity: Ref): - handlers[path] = (entity, methods.toHttpCore) - pathPrefixMaxLen = max(pathPrefixMaxLen, path.len) - do: - handlers.del(path) - pathPrefixMaxLen = 0 - for path in handlers.keys: - pathPrefixMaxLen = max(pathPrefixMaxLen, path.len) - - var parentFacet = turn.facet - proc handleRequest(req: asynchttpserver.Request): Future[void] = - # TODO: use pattern matching - var - entity: Ref - methods: set[HttpMethod] - path = req.url.splitPath() - block: - var prefix = path[0..min(pathPrefixMaxLen.succ, path.high)] - while entity.isNil: - (entity, methods) = handlers.getOrDefault(prefix) - if prefix.len == 0: break - else: discard prefix.pop() - if entity.isNil: - result = req.respond(Http503, "no handler registered for this path") - else: - var parentFut = newFuture[void]("handleRequest") - result = parentFut - if req.reqMethod notin methods: - result = req.respond(Http405, "method not valid for this handler") - else: - run(parentFacet) do (turn: var Turn): - inc requestIdSource - let - rId = requestIdSource - let rHandle = publish(turn, entity, http_protocol.Request( - handle: rId, - `method`: Method.GET, - headers: req.headers.table[], - path: path, - body: req.body)) - - onPublish(turn, entity, Response ? { 0: ?rId, 1: ?int, 3: ?string }) do (code: HttpCode, body: string): - req.respond(code, body).addCallback do (fut: Future[void]): - run(parentFacet) do (turn: var Turn): retract(turn, rHandle) - hitch(fut, parentFut) - - during(turn, ds, ?Listener) do (port: Port): - var http = newAsyncHttpServer() - asyncCheck serve(http, port, handleRequest) - do: - close(http) diff --git a/src/schema/http_protocol.nim b/src/schema/http_protocol.nim deleted file mode 100644 index 0ea6e17..0000000 --- a/src/schema/http_protocol.nim +++ /dev/null @@ -1,38 +0,0 @@ - -import - std/typetraits, preserves, std/tables - -type - Path* = seq[string] - Headers* = Table[string, seq[string]] - Response* {.preservesRecord: "http".} = object - `handle`*: BiggestInt - `code`*: BiggestInt - `headers`*: Headers - `body`*: string - - Listener* {.preservesRecord: "listen".} = object - `port`*: BiggestInt - - Handler* {.preservesRecord: "handler".} = object - `methods`*: Methods - `path`*: Path - `entity`* {.preservesEmbedded.}: Preserve[void] - - `Method`* {.preservesOr, pure.} = enum - `GET`, `HEAD`, `POST`, `PUT`, `DELETE`, `CONNECT`, `OPTIONS`, `TRACE`, - `PATCH` - Request* {.preservesRecord: "http".} = object - `handle`*: BiggestInt - `method`*: Method - `headers`*: Headers - `path`*: Path - `body`*: string - - Methods* = set[Method] -proc `$`*(x: Path | Headers | Response | Listener | Handler | Request | Methods): string = - `$`(toPreserve(x)) - -proc encode*(x: Path | Headers | Response | Listener | Handler | Request | - Methods): seq[byte] = - encode(toPreserve(x)) diff --git a/syndicate_utils.nimble b/syndicate_utils.nimble index 66aff4d..a1edd80 100644 --- a/syndicate_utils.nimble +++ b/syndicate_utils.nimble @@ -5,7 +5,7 @@ author = "Emery Hemingway" description = "Utilites for Syndicated Actors and Synit" license = "unlicense" srcDir = "src" -bin = @["http_translator", "json_socket_translator", "json_translator", "msg"] +bin = @["json_socket_translator", "json_translator", "msg"] # Dependencies