all repos — min @ 5e69999ed1c83f71991c36ac748038c94ad37016

A small but practical concatenative programming language.

Updated http server example (Thanks zenon!).
h3rald h3rald@h3rald.com
Sun, 12 Jan 2020 13:37:26 +0100
commit

5e69999ed1c83f71991c36ac748038c94ad37016

parent

342576a65fcf587348a6e913b94daaae1bb75195

1 files changed, 32 insertions(+), 22 deletions(-)

jump to
M site/contents/reference-http.mdsite/contents/reference-http.md

@@ -41,32 +41,42 @@ > > Example

> > > > The following program starts a very simple HTTP server that can display the current timestamp or date and time in ISO 8601 format: > > +> > ; Define the request handler > > ( -> > :req -> > req /url :url -> > ;Set response body -> > "Invalid Request: $1" (url) => % :body -> > ("/datetime" url ==) ( -> > timestamp datetime @body -> > ) when -> > ("/timestamp" url ==) ( -> > timestamp string @body -> > ) when -> > ("/shutdown" url ==) ( -> > "Stopping server..." puts! -> > stop-server -> > ) when -> > ;Prepare response -> > {} body %body -> > ) =handler +> > ; Assume there is a request on the stack, take it off and give it the name req +> > :req +> > ; Let's see what we got (print req to standard out) +> > "THE REQUEST:" puts! req puts! +> > ; The request is a dictionary, we retrieve the value for the key url, and give it the name url +> > req /url :url +> > "THE URL is '$1'." url quote % puts! +> > ; Constuct response body +> > ( +> > (("/datetime" url ==) (timestamp datetime)) +> > (("/timestamp" url ==) (timestamp string)) +> > (("/shutdown" url ==) ("Stopping server..." puts! stop-server)) +> > (("/" url ==) ( +> > ; this is a bit short, but works with Chrome, IE, Edge, Safari +> > "<a href='/datetime'>datetime</a>, <a href='/timestamp'>timestamp</a>, <a href='/shutdown'>stop</a>" +> > )) +> > ((true) ("Invalid Request: $1" url quote %)) +> > ) case +> > :body +> > ; Prepare the response +> > {} body %body +> > dup puts! +> > ) +> > ; The request handler is ready, give it the name handler +> > =handler +> > +> > ; Create the parameter dictionary for the server +> > {} +> > handler %handler +> > 5555 %port > > -> > ;Start server +> > ; Start server > > "Server started on port 5555." puts! > > "Press Ctrl+C to stop." puts! -> > -> > {} -> > handler %handler -> > 5555 %port > > start-server #}