Created
October 28, 2014 16:07
-
-
Save drbobbeaty/a6d6be710d340438de97 to your computer and use it in GitHub Desktop.
Need to have Gary take a look at this to see if it'll work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn return-json | |
"Creates a ring response for returning the given object as JSON." | |
([ob] (return-json ob (now) 200)) | |
([ob lastm] (return-json ob lastm 200)) | |
([ob lastm code] | |
{:status code | |
:headers {"Content-Type" "application/json; charset=UTF-8" | |
"Last-Modified" (str (or lastm (now)))} | |
;; TODO: the input-stream tactic is foiled by ring.middleware.jsonp | |
;; which slurps the whole stream before adding the callback. Could | |
;; fix by patching that lib. We should still be getting the benefit | |
;; for the non-browser case though. | |
:body (piped-input-stream | |
(bound-fn [out] | |
(with-open [osw (OutputStreamWriter. out) | |
bw (BufferedWriter. osw)] | |
(let [error-streaming | |
(fn [e] | |
;; Since the HTTP headers have already been sent, | |
;; at this point it is too late to report the error | |
;; as a 500. The best we can do is abruptly print | |
;; an error and quit. | |
(.write bw "\n\n---DEAL PERFORMANCE SERVICE ERROR WHILE STREAMING JSON---\n") | |
(.write bw (str e "\n\n")) | |
(throw e))] | |
(try | |
(json/generate-stream ob bw) | |
;; Handle "pipe closed" errors | |
(catch IOException e | |
(if (re-find #"Pipe closed" (.getMessage e)) | |
(error-streaming e))) | |
(catch Throwable t | |
(error-streaming t)))))))})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment