Skip to content

Instantly share code, notes, and snippets.

@mtnygard
Forked from ohpauleez/interceptor.clj
Created August 25, 2016 12:38
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 mtnygard/4697df5538bbd6750c153ef203924f23 to your computer and use it in GitHub Desktop.
Save mtnygard/4697df5538bbd6750c153ef203924f23 to your computer and use it in GitHub Desktop.
An example of how to make conditional interceptors that enqueue new interceptors
;; Let's start with a simple conditional interceptor that works with functions...
(defn conditional-context
"Given a keyword name and any variable predicate and terminator function pairs,
return an interceptor that will apply the terminator function paired to the first
truthy predicate. Predicates and terminators are both given the context as
the only argument.
If all predicates fail, the original context is returned."
[name-kw & pred-terms]
{:pre [(even? (count pred-terms))]}
(let [pred-term-pairs (partition 2 pred-terms)]
(interceptor/interceptor
:name name-kw
:enter (fn [context]
(or
(first
(keep (fn [[pred term]] (when (pred context)
(term context)))
pred-term-pairs))
context)))))
;; Let's write a predicate function that inspects a specific header
(defn forwarded-from-froo?
[ctx]
(= (get-in ctx [:request :headers "X-Forwarded-From") "foo.com"))
;; We can now use Pedestal's interceptor chain API to enhance it...
(require '[io.pedestal.interceptor.chain :as chain])
(defn handle-foo
"Given a context (map)
return a new context that has enqueued the additional interceptors
needed to handle Foo.com requests."
[ctx]
(chain/enqueue ctx
[
;; ... any interceptors you want in here, including another router
]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment