Skip to content

Instantly share code, notes, and snippets.

@tonsky
Created July 4, 2018 22:09
Show Gist options
  • Save tonsky/3ae1e1e319c6a3fe8d46dc6beb6e8643 to your computer and use it in GitHub Desktop.
Save tonsky/3ae1e1e319c6a3fe8d46dc6beb6e8643 to your computer and use it in GitHub Desktop.
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
Copy link

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