Last active
December 15, 2015 16:28
-
-
Save maxcountryman/5288832 to your computer and use it in GitHub Desktop.
Some macro fun.
This file contains hidden or 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
| (defmacro defroute | |
| [routename uri meths handler] | |
| `(defn | |
| ~(with-meta routename (assoc (meta routename) :route-handler true)) | |
| [req#] | |
| (let [req-meth# (:request-method req#) | |
| bad-meth# (nil? (some #(= req-meth# %) ~meths)) | |
| any-meth# (= ~meths [:any])] | |
| (if (and (route-matches ~uri req#) (and bad-meth# (not any-meth#))) | |
| (method-not-allowed req-meth# (get-allowed ~meths)) | |
| (if-let [params# (route-matches ~uri req#)] | |
| (~handler (assoc req# :route-params params#)) | |
| req#))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment