Clojure-联合收割机底座路线

Clojure-联合收割机底座路线,clojure,pedestal,Clojure,Pedestal,如何在底座中组合路线 (defroutes api-routes [...]) (defroutes site-routes [...]) (combine-routes api-routes site-routes) ;; should be a valid route as well 注意:这是一个类似于的问题,但对于底座来说。这很容易 (def all-routes (concat api-routes site-routes)) 解释从这里开始,说明 路由表只是一个数据结构;在我们的

如何在底座中组合路线

(defroutes api-routes [...])
(defroutes site-routes [...])
(combine-routes api-routes site-routes) ;; should be a valid route as well
注意:这是一个类似于的问题,但对于底座来说。

这很容易

(def all-routes (concat api-routes site-routes))
解释从这里开始,说明

路由表只是一个数据结构;在我们的例子中,这是一系列地图

底座团队将地图序列路由表形式称为详细格式,他们设计了一种简洁格式的路由表,这是我们提供给
defroute
的。
defroute
然后将简洁格式转换为详细格式

您可以在repl中自己检查

;; here we supply a terse route format to defroutes
> (defroutes routes
  [[["/" {:get home-page}
     ["/hello" {:get hello-world}]]]]) 
;;=> #'routes

;; then we pretty print the verbose route format
> (pprint routes)
;;=>
({:path-parts [""],
  :path-params [],
  :interceptors
  [{:name :mavbozo-pedestal.core/home-page,
    :enter
    #object[io.pedestal.interceptor$eval7317$fn__7318$fn__7319 0x95d91f4 "io.pedestal.interceptor$eval7317$fn__7318$fn__7319@95d91f4"],
    :leave nil,
    :error nil}],
  :path "/",
  :method :get,
  :path-re #"/\Q\E",
  :route-name :mavbozo-pedestal.core/home-page}
 {:path-parts ["" "hello"],
  :path-params [],
  :interceptors
  [{:name :mavbozo-pedestal.core/hello-world,
    :enter
    #object[io.pedestal.interceptor$eval7317$fn__7318$fn__7319 0x4a168461 "io.pedestal.interceptor$eval7317$fn__7318$fn__7319@4a168461"],
    :leave nil,
    :error nil}],
  :path "/hello",
  :method :get,
  :path-re #"/\Qhello\E",
  :route-name :mavbozo-pedestal.core/hello-world})
因此,由于基座路线只是一系列地图,我们可以轻松地将多条非重叠路线与
concat
组合在一起

这就是我喜欢的clojure的其中一个原则,该原则是Destale团队遵循的:通用数据操作,在本例中,详细格式的路由表只是一个映射——普通clojure的数据结构,可以使用常规clojure.core的数据结构操作功能(如
concat
)进行检查和操作。即使是简练的格式也是一种简单的clojure数据结构,可以用同样的方法轻松地检查和操作