[#556] Add org.httpkit.server namespace (experimental)
This commit is contained in:
parent
a248246a13
commit
a1d34f5242
13 changed files with 70 additions and 20 deletions
|
|
@ -78,11 +78,14 @@ Babashka supports the following feature flags:
|
||||||
| `BABASHKA_FEATURE_XML` | Includes the [clojure.data.xml](https://github.com/clojure/data.xml) library | `true` |
|
| `BABASHKA_FEATURE_XML` | Includes the [clojure.data.xml](https://github.com/clojure/data.xml) library | `true` |
|
||||||
| `BABASHKA_FEATURE_YAML` | Includes the [clj-yaml](https://github.com/clj-commons/clj-yaml) library | `true` |
|
| `BABASHKA_FEATURE_YAML` | Includes the [clj-yaml](https://github.com/clj-commons/clj-yaml) library | `true` |
|
||||||
| `BABASHKA_FEATURE_HTTPKIT_CLIENT` | Includes the [http-kit](https://github.com/http-kit/http-kit) client library | `true` |
|
| `BABASHKA_FEATURE_HTTPKIT_CLIENT` | Includes the [http-kit](https://github.com/http-kit/http-kit) client library | `true` |
|
||||||
|
| `BABASHKA_FEATURE_HTTPKIT_SERVER` | Includes the [http-kit](https://github.com/http-kit/http-kit) server library | `true` |
|
||||||
| `BABASHKA_FEATURE_JDBC` | Includes the [next.jdbc](https://github.com/seancorfield/next-jdbc) library | `false` |
|
| `BABASHKA_FEATURE_JDBC` | Includes the [next.jdbc](https://github.com/seancorfield/next-jdbc) library | `false` |
|
||||||
| `BABASHKA_FEATURE_POSTGRESQL` | Includes the [PostgresSQL](https://jdbc.postgresql.org/) JDBC driver | `false` |
|
| `BABASHKA_FEATURE_POSTGRESQL` | Includes the [PostgresSQL](https://jdbc.postgresql.org/) JDBC driver | `false` |
|
||||||
| `BABASHKA_FEATURE_HSQLDB` | Includes the [HSQLDB](http://www.hsqldb.org/) JDBC driver | `false` |
|
| `BABASHKA_FEATURE_HSQLDB` | Includes the [HSQLDB](http://www.hsqldb.org/) JDBC driver | `false` |
|
||||||
| `BABASHKA_FEATURE_DATASCRIPT` | Includes [datascript](https://github.com/tonsky/datascript) | `false` |
|
| `BABASHKA_FEATURE_DATASCRIPT` | Includes [datascript](https://github.com/tonsky/datascript) | `false` |
|
||||||
|
|
||||||
|
Note that httpkit server is currently experimental, the feature flag could be toggled to `false` in a future release.
|
||||||
|
|
||||||
To disable all of the above features, you can set `BABASHKA_LEAN` to `true`.
|
To disable all of the above features, you can set `BABASHKA_LEAN` to `true`.
|
||||||
|
|
||||||
### HyperSQL
|
### HyperSQL
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
(def sni-client (delay (client/make-client {:ssl-configurer sni-client/ssl-configurer})))
|
(def sni-client (delay (client/make-client {:ssl-configurer sni-client/ssl-configurer})))
|
||||||
|
|
||||||
(def sns (sci/create-ns 'org.httpkit.server nil))
|
(def sns (sci/create-ns 'org.httpkit.sni-client nil))
|
||||||
(def cns (sci/create-ns 'org.httpkit.client nil))
|
(def cns (sci/create-ns 'org.httpkit.client nil))
|
||||||
|
|
||||||
(def default-client (sci/new-dynamic-var '*default-client* sni-client {:ns cns}))
|
(def default-client (sci/new-dynamic-var '*default-client* sni-client {:ns cns}))
|
||||||
|
|
|
||||||
25
feature-httpkit-server/babashka/impl/httpkit_server.clj
Normal file
25
feature-httpkit-server/babashka/impl/httpkit_server.clj
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
(ns babashka.impl.httpkit-server
|
||||||
|
(:require [org.httpkit.server :as server]
|
||||||
|
[sci.core :as sci :refer [copy-var]]))
|
||||||
|
|
||||||
|
(def warning
|
||||||
|
"WARNING: the org.httpkit.server namespace is experimental and may be removed in future versions of babashka.
|
||||||
|
Please leave a note at https://github.com/borkdude/babashka/issues/556 to let us know how you are using it.
|
||||||
|
You can turn this warning off using -Dbabashka.httpkit-server.warning=false
|
||||||
|
This namespace will remain available under a feature flag, see https://github.com/borkdude/babashka/blob/master/doc/build.md#feature-flags")
|
||||||
|
|
||||||
|
(def sns (sci/create-ns 'org.httpkit.server
|
||||||
|
{:sci.impl/required-fn (fn [_]
|
||||||
|
(when-not (= "false" (System/getProperty "babashka.httpkit-server.warning"))
|
||||||
|
(binding [*out* *err*]
|
||||||
|
(println warning))))}))
|
||||||
|
|
||||||
|
(def httpkit-server-namespace
|
||||||
|
{:obj sns
|
||||||
|
'server-stop! (copy-var server/server-stop! sns)
|
||||||
|
'run-server (copy-var server/run-server sns)
|
||||||
|
'sec-websocket-accept (copy-var server/sec-websocket-accept sns)
|
||||||
|
'websocket-handshake-check (copy-var server/websocket-handshake-check sns)
|
||||||
|
'send-checked-websocket-handshake! (copy-var server/send-checked-websocket-handshake! sns)
|
||||||
|
'send-websocket-handshake! (copy-var server/send-websocket-handshake! sns)
|
||||||
|
'as-channel (copy-var server/as-channel sns)})
|
||||||
2
sci
2
sci
|
|
@ -1 +1 @@
|
||||||
Subproject commit 986459c139f1a12adf5dbcb7a2b5122bb4961828
|
Subproject commit 187c4551227c938090910b8ab3da024ca674c2ac
|
||||||
|
|
@ -83,6 +83,7 @@ then
|
||||||
export BABASHKA_FEATURE_JAVA_TIME=false
|
export BABASHKA_FEATURE_JAVA_TIME=false
|
||||||
export BABASHKA_FEATURE_JAVA_NIO=false
|
export BABASHKA_FEATURE_JAVA_NIO=false
|
||||||
export BABASHKA_FEATURE_HTTPKIT_CLIENT=false
|
export BABASHKA_FEATURE_HTTPKIT_CLIENT=false
|
||||||
|
export BABASHKA_FEATURE_HTTPKIT_SERVER=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
"$GRAALVM_HOME/bin/native-image" "${args[@]}"
|
"$GRAALVM_HOME/bin/native-image" "${args[@]}"
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,4 @@ export BABASHKA_CLASSPATH
|
||||||
BABASHKA_CLASSPATH=$(clojure -A:lib-tests -Spath)
|
BABASHKA_CLASSPATH=$(clojure -A:lib-tests -Spath)
|
||||||
|
|
||||||
$BB_CMD -cp "$BABASHKA_CLASSPATH:test-resources/lib_tests" \
|
$BB_CMD -cp "$BABASHKA_CLASSPATH:test-resources/lib_tests" \
|
||||||
-f "test-resources/lib_tests/babashka/run_all_libtests.clj"
|
-f "test-resources/lib_tests/babashka/run_all_libtests.clj" "$@"
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,4 @@ then
|
||||||
export PATH=$GRAALVM_HOME/bin:$PATH
|
export PATH=$GRAALVM_HOME/bin:$PATH
|
||||||
fi
|
fi
|
||||||
|
|
||||||
script/lib_tests/run_all_libtests
|
script/lib_tests/run_all_libtests "$@"
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ then
|
||||||
export BABASHKA_FEATURE_JAVA_TIME=false
|
export BABASHKA_FEATURE_JAVA_TIME=false
|
||||||
export BABASHKA_FEATURE_JAVA_NIO=false
|
export BABASHKA_FEATURE_JAVA_NIO=false
|
||||||
export BABASHKA_FEATURE_HTTPKIT_CLIENT=false
|
export BABASHKA_FEATURE_HTTPKIT_CLIENT=false
|
||||||
|
export BABASHKA_FEATURE_HTTPKIT_SERVER=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
BABASHKA_LEIN_PROFILES="+uberjar"
|
BABASHKA_LEIN_PROFILES="+uberjar"
|
||||||
|
|
@ -97,6 +98,13 @@ else
|
||||||
BABASHKA_LEIN_PROFILES+=",-feature/httpkit-client"
|
BABASHKA_LEIN_PROFILES+=",-feature/httpkit-client"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ "$BABASHKA_FEATURE_HTTPKIT_SERVER" != "false" ]
|
||||||
|
then
|
||||||
|
BABASHKA_LEIN_PROFILES+=",+feature/httpkit-server"
|
||||||
|
else
|
||||||
|
BABASHKA_LEIN_PROFILES+=",-feature/httpkit-server"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -z "$BABASHKA_JAR" ]; then
|
if [ -z "$BABASHKA_JAR" ]; then
|
||||||
lein with-profiles "$BABASHKA_LEIN_PROFILES,+reflection,-uberjar" do run
|
lein with-profiles "$BABASHKA_LEIN_PROFILES,+reflection,-uberjar" do run
|
||||||
lein with-profiles "$BABASHKA_LEIN_PROFILES" do clean, uberjar
|
lein with-profiles "$BABASHKA_LEIN_PROFILES" do clean, uberjar
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,12 @@ set BABASHKA_LEIN_PROFILES=%BABASHKA_LEIN_PROFILES%,+feature/httpkit-client
|
||||||
set BABASHKA_LEIN_PROFILES=%BABASHKA_LEIN_PROFILES%,-feature/httpkit-client
|
set BABASHKA_LEIN_PROFILES=%BABASHKA_LEIN_PROFILES%,-feature/httpkit-client
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not "%BABASHKA_FEATURE_HTTPKIT_SERVER%"=="false" (
|
||||||
|
set BABASHKA_LEIN_PROFILES=%BABASHKA_LEIN_PROFILES%,+feature/httpkit-server
|
||||||
|
) else (
|
||||||
|
set BABASHKA_LEIN_PROFILES=%BABASHKA_LEIN_PROFILES%,-feature/httpkit-server
|
||||||
|
)
|
||||||
|
|
||||||
call lein with-profiles %BABASHKA_LEIN_PROFILES% bb "(+ 1 2 3)"
|
call lein with-profiles %BABASHKA_LEIN_PROFILES% bb "(+ 1 2 3)"
|
||||||
|
|
||||||
call lein with-profiles %BABASHKA_LEIN_PROFILES%,+reflection,-uberjar do run
|
call lein with-profiles %BABASHKA_LEIN_PROFILES%,+reflection,-uberjar do run
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
(def java-time? (not= "false" (System/getenv "BABASHKA_FEATURE_JAVA_TIME")))
|
(def java-time? (not= "false" (System/getenv "BABASHKA_FEATURE_JAVA_TIME")))
|
||||||
(def java-nio? (not= "false" (System/getenv "BABASHKA_FEATURE_JAVA_NIO")))
|
(def java-nio? (not= "false" (System/getenv "BABASHKA_FEATURE_JAVA_NIO")))
|
||||||
(def httpkit-client? (not= "false" (System/getenv "BABASHKA_FEATURE_HTTPKIT_CLIENT")))
|
(def httpkit-client? (not= "false" (System/getenv "BABASHKA_FEATURE_HTTPKIT_CLIENT")))
|
||||||
|
(def httpkit-server? (not= "false" (System/getenv "BABASHKA_FEATURE_HTTPKIT_SERVER")))
|
||||||
|
|
||||||
;; excluded by default
|
;; excluded by default
|
||||||
(def jdbc? (= "true" (System/getenv "BABASHKA_FEATURE_JDBC")))
|
(def jdbc? (= "true" (System/getenv "BABASHKA_FEATURE_JDBC")))
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,9 @@
|
||||||
(when features/httpkit-client?
|
(when features/httpkit-client?
|
||||||
(require '[babashka.impl.httpkit-client]))
|
(require '[babashka.impl.httpkit-client]))
|
||||||
|
|
||||||
|
(when features/httpkit-server?
|
||||||
|
(require '[babashka.impl.httpkit-server]))
|
||||||
|
|
||||||
(sci/alter-var-root sci/in (constantly *in*))
|
(sci/alter-var-root sci/in (constantly *in*))
|
||||||
(sci/alter-var-root sci/out (constantly *out*))
|
(sci/alter-var-root sci/out (constantly *out*))
|
||||||
(sci/alter-var-root sci/err (constantly *err*))
|
(sci/alter-var-root sci/err (constantly *err*))
|
||||||
|
|
@ -404,7 +407,8 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that
|
||||||
features/transit? (assoc 'cognitect.transit @(resolve 'babashka.impl.transit/transit-namespace))
|
features/transit? (assoc 'cognitect.transit @(resolve 'babashka.impl.transit/transit-namespace))
|
||||||
features/datascript? (assoc 'datascript.core @(resolve 'babashka.impl.datascript/datascript-namespace))
|
features/datascript? (assoc 'datascript.core @(resolve 'babashka.impl.datascript/datascript-namespace))
|
||||||
features/httpkit-client? (assoc 'org.httpkit.client @(resolve 'babashka.impl.httpkit-client/httpkit-client-namespace)
|
features/httpkit-client? (assoc 'org.httpkit.client @(resolve 'babashka.impl.httpkit-client/httpkit-client-namespace)
|
||||||
'org.httpkit.sni-client @(resolve 'babashka.impl.httpkit-client/sni-client-namespace))))
|
'org.httpkit.sni-client @(resolve 'babashka.impl.httpkit-client/sni-client-namespace))
|
||||||
|
features/httpkit-server? (assoc 'org.httpkit.server @(resolve 'babashka.impl.httpkit-server/httpkit-server-namespace))))
|
||||||
|
|
||||||
(def bindings
|
(def bindings
|
||||||
{'java.lang.System/exit exit ;; override exit, so we have more control
|
{'java.lang.System/exit exit ;; override exit, so we have more control
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,20 @@
|
||||||
(:require [clojure.java.io :as io]
|
(:require [clojure.java.io :as io]
|
||||||
[clojure.test :as t]))
|
[clojure.test :as t]))
|
||||||
|
|
||||||
|
(def ns-args (set (map symbol *command-line-args*)))
|
||||||
|
|
||||||
(def status (atom {}))
|
(def status (atom {}))
|
||||||
|
|
||||||
(defn test-namespaces [& namespaces]
|
(defn test-namespaces [& namespaces]
|
||||||
(doseq [ns namespaces]
|
(let [namespaces (if (seq ns-args)
|
||||||
(require ns))
|
(keep ns-args namespaces)
|
||||||
(let [m (apply t/run-tests namespaces)]
|
namespaces)]
|
||||||
(swap! status (fn [status]
|
(prn namespaces)
|
||||||
(merge-with + status (dissoc m :type))))))
|
(doseq [ns namespaces]
|
||||||
|
(require ns))
|
||||||
|
(let [m (apply t/run-tests namespaces)]
|
||||||
|
(swap! status (fn [status]
|
||||||
|
(merge-with + status (dissoc m :type)))))))
|
||||||
|
|
||||||
;;;; clj-http-lite
|
;;;; clj-http-lite
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
(ns httpkit.client-test
|
(ns httpkit.client-test
|
||||||
(:require [cheshire.core :as json]
|
(:require [cheshire.core :as json]
|
||||||
[clojure.java.io :as io]
|
|
||||||
[clojure.string :as str]
|
|
||||||
#_:clj-kondo/ignore
|
|
||||||
[clojure.test :refer [deftest is testing #_*report-counters*]]
|
[clojure.test :refer [deftest is testing #_*report-counters*]]
|
||||||
[org.httpkit.client :as client])
|
[org.httpkit.client :as client]))
|
||||||
(:import (clojure.lang ExceptionInfo)))
|
|
||||||
|
|
||||||
(defmethod clojure.test/report :begin-test-var [m]
|
(defmethod clojure.test/report :begin-test-var [m]
|
||||||
(println "===" (-> m :var meta :name))
|
(println "===" (-> m :var meta :name))
|
||||||
|
|
@ -19,8 +15,7 @@
|
||||||
(System/exit 1))))
|
(System/exit 1))))
|
||||||
|
|
||||||
(deftest get-test
|
(deftest get-test
|
||||||
(is (str/includes? (:status @(client/get "https://postman-echo.com/get"))
|
(is (= 200 (:status @(client/get "https://postman-echo.com/get"))))
|
||||||
"200"))
|
|
||||||
(is (= "https://postman-echo.com/get"
|
(is (= "https://postman-echo.com/get"
|
||||||
(-> @(client/get "https://postman-echo.com/get"
|
(-> @(client/get "https://postman-echo.com/get"
|
||||||
{:headers {"Accept" "application/json"}})
|
{:headers {"Accept" "application/json"}})
|
||||||
|
|
@ -46,14 +41,15 @@
|
||||||
@(client/get "https://postman-echo.com/basic-auth"
|
@(client/get "https://postman-echo.com/basic-auth"
|
||||||
{:basic-auth ["postman" "password"]})))))
|
{:basic-auth ["postman" "password"]})))))
|
||||||
|
|
||||||
(deftest get-response-object-test
|
;; commented out because of httpstat.us instability
|
||||||
(let [response @(client/get "https://httpstat.us/200")]
|
#_(deftest get-response-object-test
|
||||||
|
(let [response @(client/get "https://httpstat.us/200" {:timeout 3000})]
|
||||||
(is (map? response))
|
(is (map? response))
|
||||||
(is (= 200 (:status response)))
|
(is (= 200 (:status response)))
|
||||||
(is (string? (get-in response [:headers :server]))))
|
(is (string? (get-in response [:headers :server]))))
|
||||||
|
|
||||||
(testing "response object as stream"
|
(testing "response object as stream"
|
||||||
(let [response @(client/get "https://httpstat.us/200" {:as :stream})]
|
(let [response @(client/get "https://httpstat.us/200" {:as :stream :timeout 3000})]
|
||||||
(is (map? response))
|
(is (map? response))
|
||||||
(is (= 200 (:status response)))
|
(is (= 200 (:status response)))
|
||||||
(is (instance? java.io.InputStream (:body response))))))
|
(is (instance? java.io.InputStream (:body response))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue