Skip to content

Instantly share code, notes, and snippets.

@bsless
Created September 19, 2023 16:03
Show Gist options
  • Save bsless/dd031e30af39bfc961b10686835a76fd to your computer and use it in GitHub Desktop.
Save bsless/dd031e30af39bfc961b10686835a76fd to your computer and use it in GitHub Desktop.
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
metosin/reitit {:mvn/version "0.6.0"}
info.sunng/ring-jetty9-adapter {:mvn/version "0.30.0"}
http-kit/http-kit {:mvn/version "2.7.0"}}
:aliases
{:dev {:jvm-opts ["--enable-preview"]}
:run {:exec-fn server/go
:exec-args {}}}}
<?xml version='1.0' encoding='UTF-8'?>
<configuration scanPeriod='5 seconds' scan='true'>
<appender class='ch.qos.logback.core.ConsoleAppender' name='STDOUT'>
<filter class='ch.qos.logback.classic.filter.ThresholdFilter'>
<level>
ERROR
</level>
</filter>
<encoder>
<pattern>
%date %highlight(%-5level) %logger{36}: %msg%n
</pattern>
<charset>
UTF-8
</charset>
</encoder>
</appender>
<root level='ERROR'>
</root>
<logger name='hello'>
<appender-ref ref='STDOUT'>
</appender-ref>
</logger>
<logger name='user'>
<appender-ref ref='STDOUT'>
</appender-ref>
</logger>
</configuration>
(ns server
(:import (java.util.concurrent Executors))
(:require
[reitit.ring :as ring]
[org.httpkit.server :as http]
[ring.adapter.jetty9 :refer [run-jetty]]))
(defn app []
(ring/ring-handler
(ring/router
["/hello"
{:get
{:handler
(fn ([_]
{:headers {"Content-Type" "application/text"}
:status 200
:body (do
(Thread/sleep 50)
"hello!")})
([_ r _e]
(r {:headers {"Content-Type" "application/text"}
:status 200
:body (do
(Thread/sleep 50)
"hello!")})))}}])))
(defn respond
[channel]
(fn -respond [response]
(http/send! channel response)))
(defn raise
[channel]
(fn -raise [?error]
(http/send! channel ?error)
(http/close channel)))
(defn ring->httpkit
[handler]
(fn httpkit->async [request]
(when-let [ch (request :async-channel)]
(handler request (respond ch) (raise ch))
{:body ch})))
(defn kit
[{:keys [virtual? async?]}]
(-> (app)
(cond-> async? ring->httpkit)
(http/run-server
(cond-> {:port 9999}
virtual? (assoc :worker-pool (Executors/newVirtualThreadPerTaskExecutor))))))
(defn jetty
[{:keys [virtual? async?]}]
(-> (app)
(run-jetty
(cond-> {:port 9999}
virtual? (assoc :virtual-threads? true)
async? (assoc :async? true)))))
(defn go [{:keys [server] :as opts}]
(case server
:kit (kit opts)
:jetty (jetty opts)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment