Created
September 28, 2018 12:21
-
-
Save stathissideris/179181223988eab1eaeb469461e0da79 to your computer and use it in GitHub Desktop.
Hierarchical table "syntax" for Clojure Pedestal
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
(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