I'm trying to learn compojure, and i must be missing a point: how can I
"compose" a route from multiple routes ? I have tried the following (among
other things), but I keep gettin NPE when GETting "/grou". Any advice ?
(defn hello-world [grou]
(html [:h1 (str "Hello world " grou)]))
(defroutes hello
(GET "/"
(hello-world 1)))
(defroutes hello2
(GET "/grou/"
(hello-world 2)))
(decorate hello
(with-headers {"myHeader" "myHeaderValue"})
(with-dojo-base-page "WebApp/dojo/"))
(defroutes allroutes
hello
hello2)
(defservice allroutes)
(def my-server
(jetty-server {:port 8080}
"/*" (servlet allroutes)))
I also tried to call defservice on each indivual route and defining mapping
them to separate servlets....
Thank you !
--
Ronan Dunklau
(defroutes allroutes
hello
hello2)
should be
(defroutes allroutes
(GET "/" hello)
(GET "/grou/" hello2))
For this example, it doesn't add much, but nested routes are great for
separating routes definitions. e. g.:
(defroutes blog
(GET "/" ())
(GET "/new" (new-post))
(GET "/edit" (edit-post)))
(defroutes project
(GET "/" ())
(GET "/new" (new-project))
(GET "/edit" (edit-project)))
(defroutes site
(GET "/blog/*" blog)
(GET "/projects/*" project))
I think there was a function or macro to do what you asked for though,
I just don't remember what it is and don't know if it still exists.
> signature.asc
> < 1KViewDownload
I guess I was a bit confused by a blog post which provided similar examples.
With that particular point solved, I can go back to learning compojure !
--
Ronan Dunklau
Actually, both forms should work. What may be tripping up Ronan is
that "/grou/" and "/grou" are not equivalent URIs.
It's important to realise that the GET macro is just a way of
generating a Ring handler function that returns nil if the request
doesn't match, or the user response if it does.
The defroutes macro defines a function that chains these individual
route functions together. The request map is passed to each function
in turn, until a non-nil response is returned.
You could even just write your own route functions directly:
(defroutes example
(fn [request]
(if (and (= (:request-method request) :get)
(= (:uri request) "/bar")
{:status 200
:headers {}
:body "Foobar!"})))
But because this is usually very verbose, the GET macro is probably
more suitable:
(defroutes example
(GET "/bar" "Foobar!"))
- James
and than:
(decorate allroutes append-trailing-slash)
PS: There's ignore-trailing-slash in compojure, but when you write <a
href="100001/">Object's Id</a> typing /grou you'll get href="/100001/"
and typing /grou/ you'll get href="/grou/100001/". And append-trailing-
slash does not have this issue, so IMO it's much comfortable
- Chas
--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To post to this group, send email to comp...@googlegroups.com.
To unsubscribe from this group, send email to compojure+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/compojure?hl=en.