Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created September 16, 2021 14:29
Show Gist options
  • Save souenzzo/01cec6827a1a2eedba9649191d44d292 to your computer and use it in GitHub Desktop.
Save souenzzo/01cec6827a1a2eedba9649191d44d292 to your computer and use it in GitHub Desktop.
(require '[io.pedestal.http :as http]
'[io.pedestal.test :refer [response-for]]
'[clojure.java.io :as io]
;; clojure.data.json isn't fast.
;; if you need speed, look at cheshire
'[clojure.data.json :as j])
(let [get-lazy-vs (fn []
;; just a dummy lazy get function
;; it takes 100ms to get each element
;; print each get
;; and have a limit of 10 elements
(take 10
(iterate (fn [n]
(prn [:v n])
(Thread/sleep 100)
(inc n))
0)))
hello (fn hello [req]
;; call get in handler
(let [lazy-vs (get-lazy-vs)]
(prn :returning-as-steam)
{:body (fn [out]
;; out is a outputstream;
;; you need to turn it into a writer
(with-open [w (io/writer out)]
(prn :stream-start)
(j/write lazy-vs w))
(prn :stream-end))
:status 200}))
example {:leave (fn [ctx]
(prn :leave!)
ctx)}
service-fn (-> {::http/routes #{["/" :get [example hello]
:route-name :hello]}}
http/default-interceptors
http/create-servlet
::http/service-fn)]
(response-for service-fn :get "/"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment