Skip to content

Instantly share code, notes, and snippets.

@tonsky tonsky/routing.clj
Created Jul 4, 2018

Embed
What would you like to do?
Trouble with bidi routing
;; I have this
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" :api/data-init]]])
;; And I want to make api/data-init only accesible through POST
;; I tried
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
[:post ["api/data-init" :api/data-init]]]])
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" [:post :api/data-init]]]])
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" [[:post :api/data-init]]]]])
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
{:post ["api/data-init" :api/data-init]}]])
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" {:post :api/data-init}]]])
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" [:post ["" :api/data-init]]]]])
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" {:post ["" :api/data-init]}]]])
;; Also tried replacing `:post` with `{:request-method :post}` in all those cases
;; None seemed to work
;; It either blew up on me or just didn’t matched the route on request
@malcolmsparks

This comment has been minimized.

Copy link

malcolmsparks commented Jul 10, 2018

In bidi, a route is a pair, containing a pattern and a handler. For nested routes, you can replace the handler with a vector of routes.

Line 10 fails because post in [:post ["api/data-init" :api/data-init]] is treated as a pattern, not a (method) guard.

Line 14, the [:post :api/data-init] is treated as a vector of routes, and :post is not a route.

Line 18, the 'vector of routes' contains one route, which has a single pattern of ':post', which isn't want you want.

Line 22 doesn't have a pattern.

Line 26 is the correct one:

(bidi/match-route routes "/api/data-init" :request-method :post) => {:handler :api/data-init, :request-method :post}
(bidi/match-route routes "/api/data-init" :request-method :get) => nil

Line 30, see Line 18.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.