Permalink
Browse files

Sample JSON echo service in clojure.

  • Loading branch information...
0 parents commit 7fbb4ef3adfe1ae9a9d1379728a9f2a2f3a1468a @aeden committed May 7, 2012
6 echo/clojure/echo/.gitignore
@@ -0,0 +1,6 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-failures
+.lein-deps-sum
27 echo/clojure/echo/README
@@ -0,0 +1,27 @@
+# echo
+
+Web service written in clojure that parses and echoes JSON data.
+
+## Usage
+
+Install leiningen:
+
+ brew install leiningen
+
+Download required dependencies:
+
+ lein deps
+
+Run the service:
+
+ lein exec bin/server.clj
+
+Call the service from curl:
+
+ curl -i "http://127.0.0.1:3000" -d '{"foo":"bar"}'
+
+## License
+
+Copyright (C) 2012 Anthony Eden
+
+Distributed under the MIT License.
3 echo/clojure/echo/bin/server.clj
@@ -0,0 +1,3 @@
+(use 'ring.adapter.jetty)
+(use 'echo.core)
+(run-jetty app {:port 3000})
7 echo/clojure/echo/project.clj
@@ -0,0 +1,7 @@
+(defproject echo "1.0.0-SNAPSHOT"
+ :description "Web service that parses and echoes JSON data"
+ :dependencies [[org.clojure/clojure "1.3.0"]
+ [ring/ring "1.1.0"]
+ [cheshire "4.0.0"]]
+ :dev-dependencies [[lein-exec "0.1"]
+ [ring-mock "0.1.2"]])
7 echo/clojure/echo/src/echo/core.clj
@@ -0,0 +1,7 @@
+(ns echo.core
+ (:use [cheshire.core]))
+
+(defn app [request]
+ {:status 200
+ :headers {"Content-Type" "application/json"}
+ :body (generate-string (parse-string (slurp (request :body))))})
10 echo/clojure/echo/test/echo/test/core.clj
@@ -0,0 +1,10 @@
+(ns echo.test.core
+ (:use [echo.core])
+ (:use [clojure.test])
+ (:use [ring.mock.request]))
+
+(deftest app-test
+ (is (= (app (body (request :post "/"), "{\"foo\":\"bar\"}"))
+ {:status 200
+ :headers {"Content-Type" "application/json"}
+ :body "{\"foo\":\"bar\"}"})))

0 comments on commit 7fbb4ef

Please sign in to comment.