Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all routes in compojure

How can I list all the routes on a handler function? I'm looking for behavior similar to rails' rake routes. For example:

(defroutes foo-routes
    (GET "/foo/:foo-id"
        [foo-id]
            "bar response")
    (GET "/bar/:bar-id"
        [bar-id]
            "foo response"))

Is it then possible to extract a map from foo-bar-routes containing the following?

{:GET "/foo/:foo-id"
 :GET "/bar/:bar-id"}
like image 749
nanuko Avatar asked Oct 20 '22 02:10

nanuko


1 Answers

I don't think it is possible. defroutes is a macro that returns a ring handler. GET is a macro that returns a route. Route is again just a function that calls related handler only if method and path are matching. So in the end your foo-routes is just a clojure function that is composed of other functions defined by your routes and it doesn't maintain such map. If you need to get such map, maybe you can maintain it in your code yourself and generate routes out of this map.

like image 130
Viktor K. Avatar answered Nov 29 '22 18:11

Viktor K.