Created
June 22, 2017 15:37
-
-
Save smahood/d0c8e120a28bd070b739022aeeecfec2 to your computer and use it in GitHub Desktop.
Pedestal Routing Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Types of Pedestal Routes | |
; Example A - Tabular Routes (adapted from pedestal leiningen template | |
(def routes #{["/" :get `home-page] | |
["/about" :get `about-page]}) | |
; Example B - table-routes (adapted from http://pedestal.io/reference/table-syntax#_detailed_reference) | |
(ns myapp.service | |
(:require [io.pedestal.http.route.definition.table :as table])) | |
(def application-routes | |
(table/table-routes | |
[["/" :get home-page] | |
["/about" :get about-page]])) | |
; Example C - expand-routes (adapted from http://pedestal.io/reference/table-syntax#_detailed_reference) | |
(ns myapp.service | |
(:require [io.pedestal.http.route :as route])) | |
(def application-routes | |
(route/expand-routes | |
#{["/" :get home-page] | |
["/about" :get about-page]})) | |
; Questions | |
; Q1 - In http://pedestal.io/reference/routing-quick-reference, there is an example with the following code | |
; Is there a reason this example uses syntax quoting while example C above does not? | |
(route/expand-routes | |
#{["/user/:id" :post [,,, `new-user] :constraints {:user-id #"[0-9]+"}] | |
["/user/:id" :get [,,, `view-user] :constraints {:user-id #"[0-9]+"}]}) | |
; Q2 - When referring to table-syntax, is does that mean example A or example B? |
Example A is the tabular route syntax.
Also, don't use route/expand-routes
if http/default-interceptors
is being called. This happens internally if ::http/interceptors
is not set on the service map. Your tests using response-for
may pass, but the running system will fail with an error like
clojure.lang.ExceptionInfo: Interceptor Exception: You're trying to use something as a route specification that isn't supported by the protocol; Perhaps you need to extend it?
The reason is that route/expand-routes
returns a verbose route map and the ExpandableRoutes protocol is extended to maps. When default-interceptors
is called, it tries to expand it again.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example C only works for me if I syntax quote the handler symbols or add a :route-name.