Table routes and fallback routes

54 views
Skip to first unread message

Shuai Lin

unread,
Jan 27, 2020, 10:14:49 AM1/27/20
to pedestal-users
Hi list,

Am I right that, with the table routes, it's impossible to set up "fallback routes"? 

For example, given the table routes below:

  #{
   
    ["/echo/*path"
     :get [http/json-body `echo]
     :route-name :echo]
    ["/0/*path"
     :get [http/json-body `echo]
     :route-name :0]
    ["/*path"
     :get [`home]
     :route-name :home]
}

My intention is that all unmatched routes would go to the :home route. 

However it doesn't work as I expected. For example, given the path "/0/foo", I assume it would match against the route :0, but pedestal would choose the :home route. I can see it's because the table routes is a Set so it's unordered, but that's really counter-intuitive. I tried to specify ":router :linear-search", but with no luck either.

Seems the only to achieve my goal is to use the :linear-search router with the terse routes.

Best,
Shuai

Shuai Lin

unread,
Jan 27, 2020, 10:31:41 AM1/27/20
to pedestal-users
BTW the terse syntax that works for me is like this:

[[:root
    ["/" {:get `home}
     ["/echo/*path"
      ^:interceptors [http/json-body]
      {:get `echo}]
     ["/0/*path"
      ^:interceptors [http/json-body]
      {:get [:zero-view `echo]}]
     ["/*path"
      {:get [:catch-all `home]}]]]]

Daniel De Aguiar

unread,
Jan 29, 2020, 4:12:49 PM1/29/20
to pedestal-users
Shuai Lin good question!

You are correct that using the linear search router with the terse routes routing definition is the way to go to achieve the behavior you desire. With table routes, the routes are defined in a set so ordering is not guaranteed (which generally does not matter). I'll update the docs to be more explicit about which route definition to use when such behavior is desired.

Cheers!

/dan

Paul deGrandis

unread,
Jan 30, 2020, 5:51:52 AM1/30/20
to pedestal-users

You can still use the table routes with the the linear search router.  To do so you need to specify the top-level collection as a vector instead of a set, and use the table routes function directly (instead of relying on the expand-route definition protocol).

I can try work up an example if someone doesn't beat me to it.  Hope this helps!

Cheers,
Paul

Paul deGrandis

unread,
Jan 30, 2020, 5:56:22 AM1/30/20
to pedestal-users
Just a quick note-  Pedestal discourages route setups like that (with wildcard fallbacks, etc.).  In our experience, we saw it cause more application logic and maintenance issues when used in real systems.  If possible, it's recommended you avoid that usage unless you're absolutely certain you need that functionality in your application.

Cheers,
Paul

Daniel De Aguiar

unread,
Jan 30, 2020, 11:08:24 AM1/30/20
to pedestal-users
Paul thanks for the correction and additional info! I'll work on getting this in the docs.

/dan
Reply all
Reply to author
Forward
0 new messages