Last active
December 30, 2015 20:37
-
-
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
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
service Calculator { | |
i64 sum(1: i64 a, 2: i64 b) | |
} |
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
(def transport (THttpClient. "http://localhost:3000/calc/")) | |
(def protocol (TCompactProtocol. transport)) | |
(def client (Calculator$Client. protocol)) | |
(println (.sum client 10 20)) ;; => 30 |
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 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A working example https://github.com/dimiro1/thrift-clojure-example