weavejester / compojure

A concise routing library for Ring/Clojure
Eclipse Public License 1.0
4.08k stars 259 forks source link

Record route meta data on a handler to list all defined routes #160

Closed johnmcconnell closed 7 years ago

johnmcconnell commented 7 years ago

This is useful for testing and middleware.

For example.

(defroutes app
  (GET "/h1" ...)
  (GET "h1" ...)
  ...)
(route-list app)
[[:GET "/h1"] [:GET "h2"] ... ]
(meta app)
{:route-list [[:GET "h1"] [:GET "h2"] ... ]
weavejester commented 7 years ago

The problem with this is that it's impossible to do reliably, because routes can contain other routes. I don't like the idea of introducing a feature that works sometimes, but not other times.

johnmcconnell commented 7 years ago

Can a route be expanded by concatenating the path of the parent route?

Or can we change the route list to a route tree?

Maybe I don't understand.

weavejester commented 7 years ago

Routes are Turing-complete, so there's no way of producing a reliable routing tree from a set of Compojure routes.

I have been working on Ataraxy, a data-driven syntax for routing, and I should get back to developing that soon because I intend to use it in Duct. There are also other data-driven routing syntaxes around like Bidi.

Unfortunately Compojure's core design prevents it from being transformed into a tree of routes. Compojure is powerful, but power creates opacity.

johnmcconnell commented 7 years ago

Ahh... so are the routes like "crawled" at runtime then?

johnmcconnell commented 7 years ago

I'll take a look. Thanks!

weavejester commented 7 years ago

Each route is a function that returns either a response or nil, if the route doesn't match. You can therefore have routes that are just functions that can respond to very complex conditions.

johnmcconnell commented 7 years ago

How about only supporting this feature only for handlers defined using defroutes?

I can implement it

weavejester commented 7 years ago

I'm afraid I don't want a feature in Compojure that only works only sometimes, under specific conditions. I'd rather things either work or not.

jiacai2050 commented 7 years ago

I don't think that this is a practical feature for Compojure just as @weavejester said.

If list all available routes is necessary, I think bidi is more suitable.

johnmcconnell commented 7 years ago

Cool. I'm looking to get this integrated with compojure-api because it currently has 'killer feature' of the auto generated swagger ui.

So I'm not willing to lose that feature by switching routing libraries.

I'll see if this feature is a better fit for that project. Thanks guys.