如何组合基座的路线?
(defroutes api-routes [...])
(defroutes site-routes [...])
(combine-routes api-routes site-routes) ;; should be a valid route as well注:这是一个类似的问题,但Combining routes in Compojure,但基座。
发布于 2015-11-03 14:39:35
就像
(def all-routes (concat api-routes site-routes))这个解释从这里开始,https://github.com/pedestal/pedestal/blob/master/guides/documentation/service-routing.md#defining-route-tables,据说
路由表只是一个数据结构;在我们的例子中,它是一个映射序列。
基座团队将该序列的地图路由表形式称为详细格式,他们设计了一个简洁格式的路由表,这是我们提供给defroute的。然后,defroute将我们的简洁格式转换为冗长的格式。
你可以自己检查一下
;; 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的原则之一,基座团队所遵循的原则之一:泛型数据操作--在本例中,详细格式化的路由表只是一个映射--一个普通clojure的数据结构,可以用常规的Clojure.core的数据结构操作函数(如concat )来检查和操作。即使是简洁的格式也是一个简单的clojure数据结构,并且可以用同样的方法很容易地检查和操作。
https://stackoverflow.com/questions/33495924
复制相似问题