Skip to content

Instantly share code, notes, and snippets.

Created September 16, 2021 14:29
What would you like to do?
(require '[io.pedestal.http :as http]
'[io.pedestal.test :refer [response-for]]
'[ :as io]
;; isn't fast.
;; if you need speed, look at cheshire
'[ :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))
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!)
service-fn (-> {::http/routes #{["/" :get [example hello]
:route-name :hello]}}
(response-for service-fn :get "/"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment