From 62798dc52e073f75241d42fc3beb51dc9a82f4fa Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 5 Nov 2018 11:20:59 +0000 Subject: [PATCH] A richer, more interesting HTTP server example --- packages/syntax-playground/src/server.js | 100 ++++++++++++++++++++--- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/packages/syntax-playground/src/server.js b/packages/syntax-playground/src/server.js index 700671b..196716e 100644 --- a/packages/syntax-playground/src/server.js +++ b/packages/syntax-playground/src/server.js @@ -16,31 +16,109 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- +const UI = require("@syndicate-lang/driver-browser-ui"); +// @jsx UI.html +// @jsxFrag UI.htmlFragment + +import { currentFacet, genUuid } from "@syndicate-lang/core"; + const Http = activate require("@syndicate-lang/driver-http-node"); const server = Http.HttpServer(null, 8080); +assertion type Counter(id); + +function counter() { + const id = genUuid(); + spawn named ['counter', id] { + const rootFacet = currentFacet(); + + assert Counter(id); + + field this.counter = 0; + during Http.Request($reqId, server, 'get', ['counter', id], _, _) { + assert :snapshot Http.Response( + reqId, 200, "OK", {"Content-type": "text/html"}, + '' + UI.htmlToString( +
+

The current value of counter {id} is {this.counter}.

+ +

Back to counter list.

+
+ )); + } + + during Http.Request($reqId, server, 'get', ['counter', id, 'inc'], _, _) { + on start this.counter++; + assert :snapshot Http.Response( + reqId, 303, "See other", {"Location": "/counter/" + id}, ""); + } + + during Http.Request($reqId, server, 'get', ['counter', id, 'dec'], _, _) { + on start this.counter--; + assert :snapshot Http.Response( + reqId, 303, "See other", {"Location": "/counter/" + id}, ""); + } + + during Http.Request($reqId, server, 'get', ['counter', id, 'delete'], _, _) { + on stop { rootFacet.stop(); } + assert :snapshot Http.Response( + reqId, 303, "See other", {"Location": "/"}, ""); + } + } + return id; +} + spawn named 'rootServer' { - field this.counter = 0; - during Http.Request($id, server, 'get', [], _, _) { - const v = this.counter++; - assert Http.Response(id, 200, "OK", {"Content-type": "text/plain"}, 'counter is ' + v); + let counters = {}; + on asserted Counter($id) counters[id] = true; + on retracted Counter($id) delete counters[id]; + + during Http.Request($reqId, server, 'get', [], _, _) { + const es = []; + for (let id in counters) { + es.push(
  • {id}
  • ); + } + + assert :snapshot Http.Response( + reqId, 200, "OK", {"Content-type": "text/html"}, + '' + UI.htmlToString( +
    +

    Available counters:

    +
      {es}
    +

    New counter

    +
    + )); + } + + during Http.Request($reqId, server, 'get', ['new'], _, _) { + assert :snapshot Http.Response( + reqId, 303, "See other", {"Location": "/counter/" + counter()}, ""); } } spawn named 'greetingServer' { - during Http.Request($id, server, 'get', ['hello', $name], _, _) { - assert Http.Response(id, 200, "OK", {"Content-type": "text/plain"}, "Hello, "+name+"!"); + during Http.Request($reqId, server, 'get', ['hello', $name], _, _) { + assert Http.Response(reqId, 200, "OK", {"Content-type": "text/plain"}, "Hello, "+name+"!"); + } + + during Http.Request($reqId, server, 'get', ['hello'], $query, _) { + assert Http.Response(reqId, 200, "OK", {"Content-type": "text/plain"}, + "Hello, "+query.get('name')+"!"); } } spawn named 'websocketEchoServer' { - during Http.WebSocket($id, server, ['echo'], _) { - on message Http.RequestData(id, $message) { - console.log('got', id, message); - ^ Http.ResponseData(id, message); + during Http.WebSocket($reqId, server, ['echo'], _) { + on message Http.RequestData(reqId, $message) { + console.log('got', reqId, message); + ^ Http.ResponseData(reqId, message); } - stop on message Http.RequestData(id, "quit"); + stop on message Http.RequestData(reqId, "quit"); } }