Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Last active July 2, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddeaguiar/dfcf6be6df14c14df0f868c6f420ee92 to your computer and use it in GitHub Desktop.
Save ddeaguiar/dfcf6be6df14c14df0f868c6f420ee92 to your computer and use it in GitHub Desktop.
Using Pedestal routers directly
(require '[io.pedestal.http.route.path :as route.path])
(require '[io.pedestal.http.route :as route])
(require '[io.pedestal.http.route.map-tree :as route.map-tree])
(require '[io.pedestal.http.route.router :as route.router])
;; handler fns and common-interceptors elided
(def routes #{["/" :get (conj common-interceptors `home-page)]
["/about" :get (conj common-interceptors `about-page)]
["/foo/:id" :get (conj common-interceptors `foo)]})
(def rt (route.map-tree/router (route/expand-routes routes)))
(route.router/find-route rt {:path-info "/foo/1" :request-method :get})
;; {:interceptors [#io.pedestal.interceptor.Interceptor
;; {:enter #<Fn@75b622a9 io.pedestal.interceptor.helpers/on_request[fn]>,
;; :error nil,
;; :leave nil,
;; :name :io.pedestal.http.body-params/body-params}
;; #io.pedestal.interceptor.Interceptor
;; {:enter nil,
;; :error nil,
;; :leave #<Fn@734990c1 io.pedestal.interceptor.helpers/on_response[fn]>,
;; :name :io.pedestal.http/html-body}
;; #io.pedestal.interceptor.Interceptor
;; {:enter #<Fn@75c5b7ee io.pedestal.interceptor/eval307[fn/fn]>,
;; :error nil,
;; :leave nil,
;; :name nil}],
;; :method :get,
;; :path "/foo/:id",
;; :path-constraints {:id "([^/]+)"},
;; :path-params {:id "1"},
;; :path-parts ["foo" :id],
;; :path-re #"/\Qfoo\E/([^/]+)",
;; :route-name :foo.service/foo,
;; :io.pedestal.http.route.prefix-tree/satisfies-constraints? #<Fn@5b0e805 io.pedestal.http.route.prefix_tree/add_satisfies_constraints_QMARK_[fn]>}
;; alternatively can exlude :request-method as follows
(def rt (route.map-tree/router (->> routes route/expand-routes (map #(dissoc % :method)))))
(route.router/find-route rt {:path-info "/foo/1"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment