Caleb Macdonald Black
Caleb Macdonald Black

Reputation: 1572

Pedestal early termination is not working

http://pedestal.io/reference/servlet-interceptor Says this

Before invoking the :enter functions, the servlet interceptor sets up a "terminator" predicate on the context. It terminates the interceptor chain when the context map returned by an interceptor has a response map attached.

My server has this:

(ns wp-server.server
  (:gen-class)                                              ; for -main method in uberjar
  (:require
    [io.pedestal.http :as server]
    [io.pedestal.http.route :as route]
    [wp-server.service :as service]
    [wp-server.datomic :as datomic]))

(defonce runnable-service (atom nil))

(defn -main
  "The entry-point for 'lein run'"
  [& args]
  (println "/nConnecting to datomic...")
  (datomic/connect!)
  (println "\nCreating your server...")
  (reset! runnable-service (server/create-servlet service/service))
  (server/start runnable-service))

(defn create-runnable-dev-service
  "The entry-point for 'lein run-dev'"
  [& args]
  (println "\nConnecting to [DEV] database...")
  (datomic/connect-dev!)
  (println "\nCreating your [DEV] server...")
  (-> service/service
      (merge {:env                     :dev
              ::server/join?           false
              ::server/routes          #(route/expand-routes (deref #'service/routes))
              ::server/allowed-origins {:creds true :allowed-origins (constantly true)}
              ::server/secure-headers  {:content-security-policy-settings {:object-src "none"}}})
      server/default-interceptors
      server/dev-interceptors
      server/create-servlet))


(defn start-dev []
  (when-not @runnable-service
    (reset! runnable-service (create-runnable-dev-service)))
  (server/start @runnable-service))

(defn stop-dev []
  (server/stop @runnable-service))

(defn restart-dev []
  (stop-dev)
  (start-dev))

My service looks like this:

(ns wp-server.service
  (:require
    [datomic.api :as d]
    [io.pedestal.http :as http]
    [io.pedestal.http.body-params :as body-params]
    [io.pedestal.http.route :as route]
    [ring.util.response :as ring-resp]
    [wp-server.datomic :as datomic]
    [wp-common.client :as wp-common-client]
    [wp-common.core :as wp-common-core]
    [clojure.spec.alpha :as s]
    [ring.util.response :as ring-response]))

(defn about-page
  [request]
  (ring-resp/response (format "Clojure %s - served from %s"
                              (clojure-version)
                              (route/url-for ::about-page))))

(def home-page
  {:name  :home-page
   :enter (fn [context]
            (prn "TWO")
            (assoc context :response {:status 200 :body "Hello, world!"}))})

(defn db-test-page
  [{:keys [:database]}]
  (ring-resp/response
    (prn-str
      (d/q '[:find ?text
             :where
             [?e :advisor/first-name ?text]]
           database))))

(def common-interceptors [datomic/db-interceptor (body-params/body-params) http/html-body])

(defn create-spec-validator
  [spec]
  {:name :validate-spec
   :enter
         (fn [{{:keys [:edn-params]} :request :as context}]
           (prn "ONE")
           (if-let [explained (s/explain-data spec edn-params)]
             (assoc context
               :response {:status 400 :body explained})))})


(def routes #{
              ;["/" :get (conj common-interceptors `home-page)]
              ["/clients" :post (conj common-interceptors (create-spec-validator ::wp-common-client/schema) home-page)]
              ["/db-test" :get (conj common-interceptors `db-test-page)]
              ["/about" :get (conj common-interceptors `about-page)]})

(def service {:env                     :prod
              ::http/routes            routes
              ::http/type              :jetty
              ::http/port              5000
              ::http/container-options {:h2c? true
                                        :h2?  false
                                        :ssl? false}})

When sending a request to localhost:5000/clients with a body that does not pass spec, the create-spec-validator interceptor adds a response to the context. I have confirmed this by logging the context in the home-page interceptor. I would expect the home-page interceptor to be skipped as per documentation. This does not happen. Instead the :enter function of the home-page interceptor is called and the response overwritten.

Why isn't the home-page interceptor being skipped when the create-spec-validator prior to it is returning context with a response?

Upvotes: 2

Views: 770

Answers (1)

luskwater
luskwater

Reputation: 300

If you track down the termination code, it invokes

(defn response?
  "True if the supplied value is a valid response map."
  {:added "1.1"}
  [resp]
  (and (map? resp)
       (integer? (:status resp))
       (map? (:headers resp))))

to test if there is a valid response-map in :response. Try setting an empty map in :headers in your response: it should terminate then.

Ideally, of course, you'll set Content-Type to be something meaningful.

Upvotes: 3

Related Questions