diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dc99c0b..266de275 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,11 +23,11 @@ * `reitit.ring/default-handler` now works correctly with async ring * new helper `reitit.ring/router` to compose routes outside of a router. -* `reitit.ring/create-resource-handler` function to serve static routes. See [https://metosin.github.io/reitit/ring/static.html](docs). +* `reitit.ring/create-resource-handler` function to serve static routes. See (docs)[https://metosin.github.io/reitit/ring/static.html]. ### `reitit-swagger` -* New module to produce swagger-docs from routing tree, including `Coercion` definitions. Works with both middleware & interceptors and Schema & Spec. See [https://metosin.github.io/reitit/swagger.html](docs). +* New module to produce swagger-docs from routing tree, including `Coercion` definitions. Works with both middleware & interceptors and Schema & Spec. See [docs](https://metosin.github.io/reitit/swagger.html). ```clj (require '[reitit.ring :as ring]) diff --git a/doc/ring/static.md b/doc/ring/static.md index 9ff3bbbc..a63427ff 100644 --- a/doc/ring/static.md +++ b/doc/ring/static.md @@ -19,7 +19,7 @@ This is good option if static files can be from non-conflicting paths, e.g. `"/a (ring/create-default-handler)) ``` -To serve static files with conflicting routes, e.g. `"/*#`, one needs to disable the confligt resolution: +To serve static files with conflicting routes, e.g. `"/*#`, one needs to disable the conflict resolution: ```clj (require '[reitit.ring :as ring]) @@ -59,31 +59,4 @@ To serve files from conflicting paths, e.g. `"/*"`, one option is to mount them ### TODO -* support for things like `:cache`, `:last-modified?` and `:index-files` - -## Performance - -Thanks to NIO-support, serving files is quite fast. With late2015 Macbook PRO and `[ikitommi/immutant "3.0.0-alpha1"]` here are some numbers: - -##### Small file (17 bytes) - -``` -wrk -t2 -c100 -d2s http://localhost:3000/files/hello.json -34055 requests/sec -4.64MB / sec -``` - -##### large file (406kB) - -``` -wrk -t2 -c10 -d10s http://localhost:3000/files/image.jpg -2798 request/sec -1.08GB / sec -``` - -##### single huge file (775Mb) - -``` -wget http://localhost:3000/files/LilaBali2.pptx -315 MB/s -``` +* support for things like `:cache`, `:last-modified?`, `:index-files` and `:gzip` diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index edea424c..deb064fe 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -88,24 +88,23 @@ :body file :headers {"Content-Type" (mime/ext-mime-type (.getName file) mime-types)}})] (if path - (let [path-size (count path)] + (let [path-size (count path) + serve (fn [req] + (let [uri (:uri req)] + (if (and (>= (count uri) path-size)) + (some->> (str root (subs uri path-size)) io/resource io/file response))))] (fn ([req] - (let [uri (:uri req)] - (if (and (>= (count uri) path-size)) - (some->> (str root (subs uri path-size)) io/resource io/file response)))) + (serve req)) ([req respond _] - (let [uri (:uri req)] - (if (and (>= (count uri) path-size)) - (some->> (str root (subs uri path-size)) io/resource io/file response respond)))))) - (fn - ([req] - (or (some->> req :path-params parameter (str root "/") io/resource io/file response) - {:status 404})) - ([req respond _] - (respond - (or (some->> req :path-params parameter (str root "/") io/resource io/file response) - {:status 404}))))))))) + (respond (serve req))))) + (let [serve (fn [req] + (or (some->> req :path-params parameter (str root "/") io/resource io/file response) + {:status 404}))] + (fn ([req] + (serve req)) + ([req respond _] + (respond (serve req)))))))))) (defn ring-handler "Creates a ring-handler out of a ring-router.