Created
March 12, 2011 12:36
-
-
Save tildedave/867218 to your computer and use it in GitHub Desktop.
Mock Restful Server: endpoint for your clients, just add moustache routes
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
(ns mock-restful.core | |
(:use net.cgrand.moustache | |
ring.util.response | |
ring.middleware.params | |
clojure.contrib.logging | |
[ring.adapter.jetty :only [run-jetty]])) | |
(defn remember-request [request] | |
(do | |
(log :info (format "%s %s" (:request-method request) (:uri request))) | |
request)) | |
(defn remember-requests | |
"Middleware to log all requests made to this server | |
for debugging purposes" | |
[handler & [opts]] | |
(fn [request] | |
(handler (remember-request request)))) | |
(defn contenttype-response [data type] | |
(let [wrapped-response (response data)] | |
(assoc wrapped-response :headers | |
(merge (:headers wrapped-response) | |
{"Content-Type" type})))) | |
(defn xml-response [data] | |
(contenttype-response data "application/xml")) | |
(defn serve-xml [xml] | |
(-> (slurp xml) xml-response constantly)) | |
(defn integer [s] | |
(try (Integer/parseInt s) (catch Exception e))) | |
;; TODO: generate routes from external source | |
(def my-app-handler | |
(app | |
wrap-params | |
remember-requests | |
["person" [n integer]] (serve-xml "awesome.xml"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment