Skip to content

Instantly share code, notes, and snippets.

@dimiro1
Last active December 30, 2015 20:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimiro1/0c323ef71112abc17973 to your computer and use it in GitHub Desktop.
Save dimiro1/0c323ef71112abc17973 to your computer and use it in GitHub Desktop.
Implementation of the Thrift Servlet with Clojure Ring/compojure - A Working example https://github.com/dimiro1/thrift-clojure-example
service Calculator {
i64 sum(1: i64 a, 2: i64 b)
}
(def transport (THttpClient. "http://localhost:3000/calc/"))
(def protocol (TCompactProtocol. transport))
(def client (Calculator$Client. protocol))
(println (.sum client 10 20)) ;; => 30
(defn THRIFT
"Implementation of the Thrift Servlet with compojure"
([path in-out-pfactory tprocessor] (THRIFT path in-out-pfactory in-out-pfactory tprocessor))
([path in-pfactory out-pfactory tprocessor]
(ANY path {body :body} ;; body is an InputStream
(let [in body
out (java.io.ByteArrayOutputStream.)
transport (org.apache.thrift.transport.TIOStreamTransport. in out)
processor tprocessor
in-pfactory in-pfactory
out-pfactory out-pfactory]
(.process processor
(.getProtocol in-pfactory transport)
(.getProtocol out-pfactory transport))
(.flush out)
(-> (ring.util.response/response (clojure.java.io/input-stream (.toByteArray out)))
(ring.util.response/content-type "application/x-thrift"))))))
;; Usage
(def processor (Calculator$Processor.
(reify Calculator$Iface
(sum [this a b]
(+ a b)))))
(defroutes app-routes
(GET "/" [] "Hello World")
(THRIFT "/calc/" (TCompactProtocol$Factory.) processor))
(def app
(wrap-defaults app-routes api-defaults))
@dimiro1
Copy link
Author

dimiro1 commented Dec 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment