如何对一组路由使用site-defaults中间件,对另一组路由使用api-defaults?

问题描述 投票:0回答:1

在我的Compojure / Ring Web应用程序的处理程序中,我需要使用site-defaults中间件来提供一组路由,并使用api-defaults中间件来提供另一套路由。我该怎么办?

下面的代码仅使用site-defaults中间件提供一组路由。使用api-routes中间件为第二组路由(api-defaults)提供服务时,我应该添加什么?

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

我读过这些:

在我的Compojure / Ring Web应用程序的处理程序中,我需要使用site-defaults中间件来提供一组路由,而使用api-defaults中间件来提供另一套路由。我该怎么办...

clojure compojure ring
1个回答
0
投票

我不确定在Compojure中如何解决此问题,但您不妨考虑使用Pedestal。 This page很好地介绍了路由匹配过程,该过程发生在[before

© www.soinside.com 2019 - 2024. All rights reserved.