如何將基座路線結合起來?Clojure - 結合基座路線
(defroutes api-routes [...])
(defroutes site-routes [...])
(combine-routes api-routes site-routes) ;; should be a valid route as well
注意:這是一個與Combining routes in Compojure類似的問題,但對於Pedestal。
如何將基座路線結合起來?Clojure - 結合基座路線
(defroutes api-routes [...])
(defroutes site-routes [...])
(combine-routes api-routes site-routes) ;; should be a valid route as well
注意:這是一個與Combining routes in Compojure類似的問題,但對於Pedestal。
這是那麼容易,因爲
(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 "[email protected]"],
: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 "[email protected]"],
: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數據結構,可以用相同的方法輕鬆檢查和操作。