Clojure 如何为一组路由使用站点默认中间件,为另一组路由使用api默认中间件?

Clojure 如何为一组路由使用站点默认中间件,为另一组路由使用api默认中间件?,clojure,compojure,ring,Clojure,Compojure,Ring,在我的Compojure/Ring web应用程序的处理程序中,我需要使用site defaults中间件为一组路由提供服务,并使用api defaults中间件为另一组单独的路由提供服务。我该怎么做 下面的代码仅为使用站点默认中间件的一组路由提供服务。我应该添加什么来使用api默认中间件为第二组路由api路由提供服务 (web-experiment.handler (:require [compojure.core :refer :all] [compojure.r

在我的Compojure/Ring web应用程序的处理程序中,我需要使用site defaults中间件为一组路由提供服务,并使用api defaults中间件为另一组单独的路由提供服务。我该怎么做

下面的代码仅为使用站点默认中间件的一组路由提供服务。我应该添加什么来使用api默认中间件为第二组路由api路由提供服务

(web-experiment.handler
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.middleware.defaults :refer [wrap-defaults
                                              site-defaults
                                              api-defaults]]
            [web-experiment.views :refer :all]))

(defroutes app-routes
  (GET "/" [] (index-page))
  (GET "/about" [] (about-page))
  (route/not-found "Not Found"))

(defroutes api-routes
  (GET "/grapefruit" [:as {body :body}] (grapefruit-api body))
  (GET "/factory" [:as {body :body}] (factory-api body))
  (GET "/umbrella" [:as {body :body}] (umbrella-api body))
  (route/not-found "Not Found"))

(def app
  (wrap-defaults app-routes site-defaults))
;; TODO: Add api-routes. How to use api-defaults middleware to serve api-routes?
我读过:

-无法解决问题,因为提供的解决方案无法与使用站点默认配置的wrap defaults中间件一起工作

-没有为我的问题提供明确的解决方案,即代码片段


我不确定如何在CopJuje中解决这个问题,但是你可能希望考虑使用基座。对路由匹配过程进行了很好的介绍,该过程发生在任何拦截器被称为环形中间件的基座替换之前

因此,您可以定义两组不同的中间件:

(def user-intc-chain [inject-connection auth-required (body-params/body-params)] )
(def api-intc-chain  [inject-connection auth-required api-params-intc] )
然后定义路线,如:

["/echo"   :get (conj user-intc-chain echo-intc) ] ; a "user" route
["/alive?" :get (conj api-intc-chain alive-intc) ] ; an "api" route

除了内置的底座功能外,我在这里还提供了许多帮助和便利功能:

您可以将一些路由包装在一个包装器中,而将其他路由包装在另一个包装器中。你只需要稍微改变一下你的路线

不久前,我写了一个演示如何使用环形路由。你可以在

特别是从演示开始的部分,您应该会感兴趣