Pedestal でルートを組み合わせるにはどうすればよいですか?
(defroutes api-routes [...])
(defroutes site-routes [...])
(combine-routes api-routes site-routes) ;; should be a valid route as well
注 : これはCompojure でのルートの結合と同様の質問ですが、台座の場合です。
Pedestal でルートを組み合わせるにはどうすればよいですか?
(defroutes api-routes [...])
(defroutes site-routes [...])
(combine-routes api-routes site-routes) ;; should be a valid route as well
注 : これはCompojure でのルートの結合と同様の質問ですが、台座の場合です。
それは同じくらい簡単です
(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
、簡潔な形式を詳細な形式に変換します。
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 の原則の 1 つについて私が気に入っていることです:この場合、詳細なフォーマットのルート テーブルは単なるマップである一般的なデータ操作です。通常の clojure.core のデータ構造で検査および操作できます。などのデータ構造操作関数concat
。簡潔な形式でさえ、単純な clojure データ構造でもあり、同じ手段で簡単に検査および操作できます。