diff --git a/doc/build.md b/doc/build.md index a8bbf58a..a2159ab8 100644 --- a/doc/build.md +++ b/doc/build.md @@ -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_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_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_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_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`. ### HyperSQL diff --git a/feature-httpkit-client/babashka/impl/httpkit_client.clj b/feature-httpkit-client/babashka/impl/httpkit_client.clj index 310c9a42..bb3e10ea 100644 --- a/feature-httpkit-client/babashka/impl/httpkit_client.clj +++ b/feature-httpkit-client/babashka/impl/httpkit_client.clj @@ -9,7 +9,7 @@ (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 default-client (sci/new-dynamic-var '*default-client* sni-client {:ns cns})) diff --git a/feature-httpkit-server/babashka/impl/httpkit_server.clj b/feature-httpkit-server/babashka/impl/httpkit_server.clj new file mode 100644 index 00000000..46bcdbdc --- /dev/null +++ b/feature-httpkit-server/babashka/impl/httpkit_server.clj @@ -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)}) diff --git a/sci b/sci index 986459c1..187c4551 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 986459c139f1a12adf5dbcb7a2b5122bb4961828 +Subproject commit 187c4551227c938090910b8ab3da024ca674c2ac diff --git a/script/compile b/script/compile index f13b067d..c8dcb31b 100755 --- a/script/compile +++ b/script/compile @@ -83,6 +83,7 @@ then export BABASHKA_FEATURE_JAVA_TIME=false export BABASHKA_FEATURE_JAVA_NIO=false export BABASHKA_FEATURE_HTTPKIT_CLIENT=false + export BABASHKA_FEATURE_HTTPKIT_SERVER=false fi "$GRAALVM_HOME/bin/native-image" "${args[@]}" diff --git a/script/lib_tests/run_all_libtests b/script/lib_tests/run_all_libtests index 03a1f42e..c829b0cd 100755 --- a/script/lib_tests/run_all_libtests +++ b/script/lib_tests/run_all_libtests @@ -12,4 +12,4 @@ export BABASHKA_CLASSPATH BABASHKA_CLASSPATH=$(clojure -A:lib-tests -Spath) $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" "$@" diff --git a/script/run_lib_tests b/script/run_lib_tests index c91c845c..f94180a4 100755 --- a/script/run_lib_tests +++ b/script/run_lib_tests @@ -8,4 +8,4 @@ then export PATH=$GRAALVM_HOME/bin:$PATH fi -script/lib_tests/run_all_libtests +script/lib_tests/run_all_libtests "$@" diff --git a/script/uberjar b/script/uberjar index 9f19360d..6b55f648 100755 --- a/script/uberjar +++ b/script/uberjar @@ -23,6 +23,7 @@ then export BABASHKA_FEATURE_JAVA_TIME=false export BABASHKA_FEATURE_JAVA_NIO=false export BABASHKA_FEATURE_HTTPKIT_CLIENT=false + export BABASHKA_FEATURE_HTTPKIT_SERVER=false fi BABASHKA_LEIN_PROFILES="+uberjar" @@ -97,6 +98,13 @@ else BABASHKA_LEIN_PROFILES+=",-feature/httpkit-client" 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 lein with-profiles "$BABASHKA_LEIN_PROFILES,+reflection,-uberjar" do run lein with-profiles "$BABASHKA_LEIN_PROFILES" do clean, uberjar diff --git a/script/uberjar.bat b/script/uberjar.bat index 4866e189..735abbe5 100755 --- a/script/uberjar.bat +++ b/script/uberjar.bat @@ -70,6 +70,12 @@ 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%,+reflection,-uberjar do run diff --git a/src/babashka/impl/features.clj b/src/babashka/impl/features.clj index 5c7c478c..f02d5784 100644 --- a/src/babashka/impl/features.clj +++ b/src/babashka/impl/features.clj @@ -10,6 +10,7 @@ (def java-time? (not= "false" (System/getenv "BABASHKA_FEATURE_JAVA_TIME"))) (def java-nio? (not= "false" (System/getenv "BABASHKA_FEATURE_JAVA_NIO"))) (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 (def jdbc? (= "true" (System/getenv "BABASHKA_FEATURE_JDBC"))) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index d8491fdb..8ddc66d7 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -84,6 +84,9 @@ (when features/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/out (constantly *out*)) (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/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) - '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 {'java.lang.System/exit exit ;; override exit, so we have more control diff --git a/test-resources/lib_tests/babashka/run_all_libtests.clj b/test-resources/lib_tests/babashka/run_all_libtests.clj index 75a0d3b4..44860a69 100644 --- a/test-resources/lib_tests/babashka/run_all_libtests.clj +++ b/test-resources/lib_tests/babashka/run_all_libtests.clj @@ -2,14 +2,20 @@ (:require [clojure.java.io :as io] [clojure.test :as t])) +(def ns-args (set (map symbol *command-line-args*))) + (def status (atom {})) (defn test-namespaces [& namespaces] - (doseq [ns namespaces] - (require ns)) - (let [m (apply t/run-tests namespaces)] - (swap! status (fn [status] - (merge-with + status (dissoc m :type)))))) + (let [namespaces (if (seq ns-args) + (keep ns-args namespaces) + namespaces)] + (prn namespaces) + (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 diff --git a/test-resources/lib_tests/httpkit/client_test.clj b/test-resources/lib_tests/httpkit/client_test.clj index 32727306..8a15d811 100644 --- a/test-resources/lib_tests/httpkit/client_test.clj +++ b/test-resources/lib_tests/httpkit/client_test.clj @@ -1,11 +1,7 @@ (ns httpkit.client-test (: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*]] - [org.httpkit.client :as client]) - (:import (clojure.lang ExceptionInfo))) + [org.httpkit.client :as client])) (defmethod clojure.test/report :begin-test-var [m] (println "===" (-> m :var meta :name)) @@ -19,8 +15,7 @@ (System/exit 1)))) (deftest get-test - (is (str/includes? (:status @(client/get "https://postman-echo.com/get")) - "200")) + (is (= 200 (:status @(client/get "https://postman-echo.com/get")))) (is (= "https://postman-echo.com/get" (-> @(client/get "https://postman-echo.com/get" {:headers {"Accept" "application/json"}}) @@ -46,14 +41,15 @@ @(client/get "https://postman-echo.com/basic-auth" {:basic-auth ["postman" "password"]}))))) -(deftest get-response-object-test - (let [response @(client/get "https://httpstat.us/200")] +;; commented out because of httpstat.us instability +#_(deftest get-response-object-test + (let [response @(client/get "https://httpstat.us/200" {:timeout 3000})] (is (map? response)) (is (= 200 (:status response))) (is (string? (get-in response [:headers :server])))) (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 (= 200 (:status response))) (is (instance? java.io.InputStream (:body response))))))