web

A web framework for Carp with routing, JSON integration, WebSocket and Server-Sent Events support, and concurrent connection handling.

Define handlers as (Fn [&Request &(Map String String)] Response) functions, register them as routes on an App, and start serving with App.serve.

HTTP Example

(load "git@github.com:carpentry-org/web@0.10.0")

(defn hello [req params]
  (Response.text @"Hello, world!"))

(defserver "0.0.0.0" 8080
  (GET "/" hello))

WebSocket Example

(defn echo [event params ws]
  (match-ref event
    (WSEvent.Connect) (WebSocket.send ws "connected")
    (WSEvent.Message msg) (WebSocket.send ws &(fmt "echo: %s" msg))
    (WSEvent.Close) ()))

(defserver "0.0.0.0" 8080
  (WS "/ws/echo" echo))

Server-Sent Events Example

(defn clock [event params s]
  (match-ref event
    (SSEEvent.Connect) (SSEStream.send-event s "ready" "")
    (SSEEvent.Tick) (SSEStream.send s &(Int.str (System.time)))
    (SSEEvent.Close) ()))

(defserver "0.0.0.0" 8080
  (SSE "/events" clock))