Getting started
Plumeria is a keep-alive HTTP/1.1 server for Clojure on the Jolt runtime (Chez Scheme), with one Jolt fiber per connection. It doesn't run on the JVM.
Install Jolt
You need Jolt 0.8.15 or newer. Plumeria is tested on 0.8.32. Plain HTTP needs nothing else: no libuv and no native libraries. HTTPS needs OpenSSL 3 (see https.md).
brew install jolt-lang/jolt/jolt # or see the Jolt README for other installs
jolt --version
In a checkout of this repo, jolt doctor checks that your jolt has what plumeria needs.
Add it to a project
There are no releases yet, so depend on a commit:
{:deps {io.github.jlt-commons/plumeria {:git/url "https://github.com/jlt-commons/plumeria"
:git/sha "<commit>"}}}
Options, defaults and namespaces may still change between commits, so pin one and read the log before you move it.
A first server
(require '[plumeria.core :as http])
(def server
(http/run-server
(fn [req]
{:status 200
:headers {"Content-Type" "text/plain"}
:body (str "you asked for " (:uri req) "\n")})
{:port 18090}))
;; curl http://127.0.0.1:18090/hi -> you asked for /hi
(http/stop-server server)
run-server returns at once with a handle {:port :host :stop}. :port is the port the server bound, so {:port 0} takes any free one. The server binds 127.0.0.1 unless you pass :host "0.0.0.0". stop-server stops accepting, lets in-flight requests finish, closes idle connections, and returns once they're all gone.
http-listen is the same server with the port as an argument and a bare stop fn in place of the handle:
(def stop! (http/http-listen 18090 (fn [req] {:status 200 :body "hi\n"})))
(stop!)
Both take an options map for the limits and timeouts (limits-and-timeouts.md) and every feature in this guide.
The repo has two demo servers in examples/:
jolt -m hello-server # curl http://127.0.0.1:18090/hi
jolt -m routed-server # routing and JSON: curl http://127.0.0.1:18090/json
The socket layer on its own
Underneath, plumeria.socket has listen!, accept!, read!, write! and close!. Called on a fiber, the ones that could block park the fiber instead, and Jolt resumes it when the socket is ready. So this echo server reads like blocking code, yet holds many connections without a thread for each:
(ns my.echo
(:require [plumeria.socket :as socket]
[jolt.fibers :as fib]))
(defn -main [& _]
(let [server (socket/listen! "0.0.0.0" 18091)]
(loop []
(when-let [client (socket/accept! server)] ; parks until a client connects
(fib/spawn ; one fiber per connection
(fn []
(let [data (socket/read! client 5000)] ; parks until data, :eof, or 5 s
(when-not (= data :eof)
(socket/write! client data)) ; parks until written
(socket/close! client))))
(recur)))))
read! answers the bytes it read, or :eof when the peer closed, the read failed, or the timeout passed. write! answers true or false. Neither throws, so callers branch on the value.
Where next
- requests-and-responses.md: the Ring request and response, streaming, routing, proxies, ring-core middleware.
- contributing.md: the tasks, the test suite and the checks a change needs.