基座提前终止不起作用



http://pedestal.io/reference/servlet-interceptor 这样说

在调用 :enter 函数之前,servlet 拦截器在上下文上设置一个"终止符"谓词。当拦截器返回的上下文映射附加了响应映射时,它会终止拦截器链。

我的服务器有这个:

(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))

我的服务如下所示:

(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}})

当向 localhost:5000/客户端发送正文未传递规范的请求时,创建规范验证器拦截器会向上下文添加响应。我已经通过在主页拦截器中记录上下文来确认这一点。我希望根据文档跳过主页拦截器。这不会发生。而是调用主页拦截器的 :enter 函数并覆盖响应。

为什么当主页拦截器之前的创建规范验证器返回带有响应的上下文时,它没有被跳过?

如果您跟踪终止代码,它会调用

(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))))

以测试:response中是否存在有效的响应映射。尝试在响应中:headers设置一个空地图:它应该终止。

当然,理想情况下,你会把Content-Type设置为有意义的东西。

相关内容

  • 没有找到相关文章

最新更新