Merge pull request #19 from cprice404/feature/master/route-resources
(MAINT) added 'resources' route utility fn
Chris Price
8 years ago
5 | 5 |
|
6 | 6 |
:dependencies [[org.clojure/clojure "1.6.0"]
|
7 | 7 |
[org.clojure/tools.reader "0.8.9"]
|
|
8 |
[clj-time "0.7.0"]
|
|
9 |
|
8 | 10 |
[ring/ring-core "1.3.2"]
|
9 | 11 |
[commons-io "2.4"]
|
10 | 12 |
[bidi "1.21.0" :exclusions [org.clojure/clojurescript]]
|
4 | 4 |
[clojure.zip :as zip]
|
5 | 5 |
[compojure.core :as compojure]
|
6 | 6 |
[compojure.response :as compojure-response]
|
|
7 |
[ring.util.mime-type :as mime]
|
7 | 8 |
[ring.util.response :as ring-response]
|
8 | 9 |
[schema.core :as schema]
|
9 | 10 |
[puppetlabs.kitchensink.core :as ks]
|
|
249 | 250 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
250 | 251 |
;;; Private - helpers for compojure-like syntax
|
251 | 252 |
|
|
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 |
|
252 | 258 |
(defmacro handler-fn*
|
253 | 259 |
"Helper macro, used by the compojure-like macros (GET/POST/etc.) to generate
|
254 | 260 |
a function that provides compojure's destructuring and rendering support."
|
|
360 | 366 |
[[[#".*" :rest]] (fn [request]
|
361 | 367 |
(-> (compojure-response/render body request)
|
362 | 368 |
(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)))))
|