Skip to content

Instantly share code, notes, and snippets.

@tildedave
Created March 12, 2011 12:36
Show Gist options
  • Save tildedave/867218 to your computer and use it in GitHub Desktop.
Save tildedave/867218 to your computer and use it in GitHub Desktop.
Mock Restful Server: endpoint for your clients, just add moustache routes
(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