Permalink
Please sign in to comment.
Browse files
Introduce ring.branch, add both ring.branch and related ring.static t…
…o README.
- Loading branch information...
Showing
with
42 additions
and 0 deletions.
- +2 −0 README.textile
- +24 −0 src/ring/branch.clj
- +15 −0 test/ring/branch_test.clj
- +1 −0 test/run.clj
2
README.textile
24
src/ring/branch.clj
| @@ -0,0 +1,24 @@ | ||
| +(ns ring.branch) | ||
| + | ||
| +(defn wrap | ||
| + "Returns an app that proxies to one of several possible backing apps depending | ||
| + on the beginning of the request uri. | ||
| + Should be passed an odd number of arguments - the inital pairs of arguments | ||
| + indicate the branching conditions and corresponding apps, and the last | ||
| + argument the fallback app that will be invoked if none of the branching | ||
| + conditions are met: | ||
| + | ||
| + (wrap [\"/javascripts\" \"stylesheets\"] static-app | ||
| + [\"/blog\"] blog-app | ||
| + dynamic-app)" | ||
| + [& branching] | ||
| + (let [fallback (last branching) | ||
| + branch-logic | ||
| + (map (fn [[prefixes branch]] | ||
| + (fn [req] | ||
| + (let [uri (:uri req)] | ||
| + (if (some #(.startsWith uri %) prefixes) (branch req))))) | ||
| + (partition 2 branching))] | ||
| + (fn [req] | ||
| + (or (some #(% req) branch-logic) | ||
| + (fallback req))))) |
15
test/ring/branch_test.clj
| @@ -0,0 +1,15 @@ | ||
| +(ns ring.branch-test | ||
| + (:use clj-unit.core ring.branch)) | ||
| + | ||
| +(def wrapped | ||
| + (wrap ["/javascripts" "/stylesheets"] (fn [req] [req :static]) | ||
| + ["/blog"] (fn [req] [req :blog]) | ||
| + (fn [req] [req :dynamic]))) | ||
| + | ||
| +(deftest "wrap" | ||
| + (let [req {:uri "/stylesheets"}] | ||
| + (assert= [req :static] (wrapped req))) | ||
| + (let [req {:uri "/blog/foo"}] | ||
| + (assert= [req :blog] (wrapped req))) | ||
| + (let [req {:uri "/foo/blog"}] | ||
| + (assert= [req :dynamic] (wrapped req)))) |
1
test/run.clj
0 comments on commit
843c0a9