Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Last active December 15, 2015 16:28
Show Gist options
  • Select an option

  • Save maxcountryman/5288832 to your computer and use it in GitHub Desktop.

Select an option

Save maxcountryman/5288832 to your computer and use it in GitHub Desktop.
Some macro fun.
(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