Codebase list comidi-clojure / 8c10c27
Merge pull request #19 from cprice404/feature/master/route-resources (MAINT) added 'resources' route utility fn Chris Price 8 years ago
2 changed file(s) with 19 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
55
66 :dependencies [[org.clojure/clojure "1.6.0"]
77 [org.clojure/tools.reader "0.8.9"]
8 [clj-time "0.7.0"]
9
810 [ring/ring-core "1.3.2"]
911 [commons-io "2.4"]
1012 [bidi "1.21.0" :exclusions [org.clojure/clojurescript]]
44 [clojure.zip :as zip]
55 [compojure.core :as compojure]
66 [compojure.response :as compojure-response]
7 [ring.util.mime-type :as mime]
78 [ring.util.response :as ring-response]
89 [schema.core :as schema]
910 [puppetlabs.kitchensink.core :as ks]
249250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250251 ;;; Private - helpers for compojure-like syntax
251252
253 (defn- add-mime-type [response path options]
254 (if-let [mime-type (mime/ext-mime-type path (:mime-types options {}))]
255 (ring-response/content-type response mime-type)
256 response))
257
252258 (defmacro handler-fn*
253259 "Helper macro, used by the compojure-like macros (GET/POST/etc.) to generate
254260 a function that provides compojure's destructuring and rendering support."
360366 [[[#".*" :rest]] (fn [request]
361367 (-> (compojure-response/render body request)
362368 (ring-response/status 404)))])
369
370 (defn resources
371 "A route for serving resources on the classpath. Accepts the following
372 keys:
373 :root - the root prefix path of the resources, defaults to 'public'
374 :mime-types - an optional map of file extensions to mime types"
375 [path & [options]]
376 (GET [path [#".*" :resource-path]] [resource-path]
377 (let [root (:root options "public")]
378 (some-> (ring-response/resource-response (str root "/" resource-path))
379 (add-mime-type resource-path options)))))