1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# Abra
A small [clojure][1] library for reverse-routing in [compojure][2]
applications.
[1]: http://clojure.org
[2]: https://github.com/weavejester/compojure
# Usage
Include `abra` by adding the following to your dependencies:
:::clojure
[org.clojars.czan/abra "0.1.0"]
At the very bottom of your middleware stack (closest to your actual
handlers), apply the `abra.core/wrap-reverse-routing` middleware (any
lower-level middleware must then be applied with
`abra.core/add-middleware`).
Instead of `compojure.core/`{`defroutes`,`routes`,`context`}, use
`abra.core/`{`defroutes`,`routes`,`context`}.
As an example:
:::clojure
(require '[abra.core :refer [routes context] :as abra])
(def app (-> (routes (context "/api" []
(-> #'api-routes
(abra/add-middleware api)))
(-> #'site-routes
(abra/add-middleware site)))
abra/wrap-reverse-routing))
When giving a route a name, wrap it in a call to
`abra.core/register-route`:
:::clojure
(defroutes site-routes
(register-route :username
(GET "/username/" []
(str "a username"))))
`register-route` currently only supports the standard `compojure`
methods: `GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTIONS`, `PATCH`
and `ANY`.
To later retrieve this url, use the `abra.core/url-for` function:
:::clojure
(defroutes api-routes
(GET "/" []
(str "url: " (url-for :username))))
The `url-for` function at present does not indicate the method to be
used for the url, although this information may be exposed in future.
A route may also require parameters, in which case the `url-for`
function must be provided the correct number of additional parameters:
:::clojure
(defroutes test-routes
(context "/user/:id" [id]
(register-route :user-attr
(GET "/:attr" [attr]
(str id "::" attr))))
(GET "/" []
(str (url-for :user-attr 10 "name"))))
|