Clojure ring adapter for
com.sun.net.httpserver.HttpServer
which is included in the JDK.
The main motivation for this is to support starting a small HTTP server inside an application which itself isn't necessary primarily a web app, while avoiding adding any new major dependencies on the classpath.
Installation
Add ring-http-exchange to dependency list
[org.clojars.jj/ring-http-exchange "1.4.9"]Usage
(:require [ring-http-exchange.core :as server])
(server/run-http-server (fn [_] {:status 200 :headers {"Content-Type" "text/html; charset=utf-8"} :body "hello world"}) {:port 8080 :ssl-context (ssl/keystore->ssl-context (io/resource "keystore.jks") "keystore-password" (io/resource "truststore.jks") "truststore-password") :executor (Executors/newVirtualThreadPerTaskExecutor)})
CompletableFuture handler
(import '(java.util.concurrent CompletableFuture)) (server/run-http-server (fn [req] (CompletableFuture/completedFuture {:status 200 :headers {"Content-Type" "text/html; charset=utf-8"} :body "hello world"})) {:port 8080 :handler-mode :future})
Supported response body types
| Response Body Type |
|---|
java.lang.String |
java.io.InputStream |
java.io.File |
byte[] |
ring.core.protocols/StreamableResponseBody |
Server configuration
| Property | Description | Default value |
|---|---|---|
host |
Host name | 0.0.0.0 |
port |
Application port | 8080 |
executor |
Executor to be used | newThreadPerTaskExecutor |
ssl-context |
Ssl context to be used in https configurator | nil |
record-support? |
Can use records as a response | true |
handler-mode |
:sync, :async (callback), or :future (CompletableFuture) |
:sync |
request-map-fields |
Set of keys to include in request map | nil (all fields) |
Performance Tips
Robaho httpserver
Robaho httpserver can be added to the dependency list,
which is a drop-in replacement for a com.sun.net.httpserver
[io.github.robaho/httpserver "1.0.29"]License
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.