Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Last active March 24, 2017 19:52
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 ddeaguiar/74eed4784848e49fa83179a2086aa9ea to your computer and use it in GitHub Desktop.
Save ddeaguiar/74eed4784848e49fa83179a2086aa9ea to your computer and use it in GitHub Desktop.
interceptor short-circuiting
(ns terminator.service
(:require [io.pedestal.http :as http]
[io.pedestal.http.route :as route]
[io.pedestal.http.body-params :as body-params]
[io.pedestal.interceptor :as i]
[ring.util.response :as ring-resp]))
(defn about-page
[request]
(ring-resp/response (format "Clojure %s - served from %s"
(clojure-version)
(route/url-for ::about-page))))
(defn home-page
[request]
(io.pedestal.log/info :msg "In Home Page!")
(ring-resp/response "Hello World!"))
(def terminator
(i/interceptor
{:name ::terminator
:enter (fn [ctx]
(io.pedestal.log/info :msg "In Terminator")
(if (= "y" (get-in ctx [:request :params :t]))
(assoc ctx :response (ring-resp/response "Terminated!"))
ctx))
:leave (fn [ctx]
(io.pedestal.log/info :msg "Leaving Terminator")
ctx)}))
(def terminator2
(i/interceptor
{:name ::terminator2
:enter (fn [ctx]
(io.pedestal.log/info :msg "In Terminator2")
(if (= "yy" (get-in ctx [:request :params :t]))
(assoc ctx :response (ring-resp/response "Terminated 2!"))
ctx))
:leave (fn [ctx]
(io.pedestal.log/info :msg "Leaving Terminator2")
ctx)}))
;; Defines "/" and "/about" routes with their associated :get handlers.
;; The interceptors defined after the verb map (e.g., {:get home-page}
;; apply to / and its children (/about).
(def common-interceptors [(body-params/body-params) http/html-body])
;; Tabular routes
(def routes #{["/" :get (into common-interceptors [`terminator `terminator2 `home-page])]
["/about" :get (conj common-interceptors `about-page)]})
;; Map-based routes
;(def routes `{"/" {:interceptors [(body-params/body-params) http/html-body]
; :get home-page
; "/about" {:get about-page}}})
;; Terse/Vector-based routes
;(def routes
; `[[["/" {:get home-page}
; ^:interceptors [(body-params/body-params) http/html-body]
; ["/about" {:get about-page}]]]])
;; Consumed by terminator.server/create-server
;; See http/default-interceptors for additional options you can configure
(def service {:env :prod
;; You can bring your own non-default interceptors. Make
;; sure you include routing and set it up right for
;; dev-mode. If you do, many other keys for configuring
;; default interceptors will be ignored.
;; ::http/interceptors []
::http/routes routes
;; Uncomment next line to enable CORS support, add
;; string(s) specifying scheme, host and port for
;; allowed source(s):
;;
;; "http://localhost:8080"
;;
;;::http/allowed-origins ["scheme://host:port"]
;; Root for resource interceptor that is available by default.
::http/resource-path "/public"
;; Either :jetty, :immutant or :tomcat (see comments in project.clj)
::http/type :jetty
;;::http/host "localhost"
::http/port 8080
;; Options to pass to the container (Jetty)
::http/container-options {:h2c? true
:h2? false
;:keystore "test/hp/keystore.jks"
;:key-password "password"
;:ssl-port 8443
:ssl? false}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment