Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Created September 28, 2018 12:21
Show Gist options
  • Save stathissideris/179181223988eab1eaeb469461e0da79 to your computer and use it in GitHub Desktop.
Save stathissideris/179181223988eab1eaeb469461e0da79 to your computer and use it in GitHub Desktop.
Hierarchical table "syntax" for Clojure Pedestal
(defn- add-path-prefix [[path & more] prefix]
(vec (concat [(str prefix path)] more))) ;;TODO handle stray slashes
(defn- prepend-interceptors [[path verb chain & more] interceptors]
(let [new-chain (if (vector? chain)
(vec (concat interceptors chain))
(vec (concat interceptors [chain])))]
(vec (concat [path verb new-chain] more))))
(defn- group
([prefix routes]
(group prefix [] routes))
([prefix interceptors routes]
(->> routes
(map #(add-path-prefix % prefix))
(map #(prepend-interceptors % interceptors))
(set))))
(defn routes [db]
(route/expand-routes
(group "/api" [(inter/inject-db db)
(body-params)
(inter/rename-body-params)]
#{["/status" :get handlers/status]
["/foo" :get handlers/foo]
["/hello" :get handlers/hello]
["/adder/:a/:b" :get [(coerce {:a int? :b int?}) handlers/adder] :constraints {:a numeric :b numeric}]
["/echo" :get handlers/echo]
["/pod" :put pod/create-pod]
["/invoice/pod/:code" :get pod/get-pod]})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment