2015-07-03 26 views
3

我在嘗試匹配以下形式的路線:{{mongoID}}.{{width}}x{{height}}.{{extension}} 例如,/5591499e2dbc18bd0f000050.240x240.jpeg是有效的路線。在Compojure/Clout中匹配路線

我希望能夠解構它,像這樣:

{:id  5591499e2dbc18bd0f000050 
:width  240 
:height 240 
:extension jpeg } 

Compojure支持正則表達式,並點太明顯https://github.com/weavejester/compojure/issues/42

我可以有單獨的正則表達式的每個字段的,但我不知道如何將它放入路由路徑(我試圖使用數組語法): https://github.com/weavejester/compojure/wiki/Routes-In-Detail#matching-the-uri

比方說,我有這樣的:

(GET ["/my-route/:mongoID.:widthx:height.:extension" :mongoID ... 
                :width ... 
                :height ... 
                :extension ...]) 

顯然字符串"/my-route/:mongoID.:widthx:height.:extension"將無法​​正常工作(只是因爲「X」是輸了,也許別的東西太)。

我該如何修改我的路線以使其符合我的論點?

注:如果這很有用,我還使用棱鏡/架構。

回答

4

Compojure使用clout進行路由匹配。這就是它允許你爲每個參數指定正則表達式的方式。在影響力以下工作:

user=> (require '[clout.core :as clout]) 
user=> (require '[ring.mock.request :refer [request]]) 
user=> (clout/route-matches (clout/route-compile "/my-route/:mongoID.:width{\\d+}x:height{\\d+}.:extension") (request :get "/my-route/5591499e2dbc18bd0f000050.240x240.jpeg")) 
{:extension "jpeg", :height "240", :width "240", :mongoID "5591499e2dbc18bd0f000050"} 

所以下面應該工作的Compojure:

(GET "/my-route/:mongoID.:width{\\d+}x:height{\\d+}.:extension" 
    [mongoID width height extension] 
    (do-something-with mongoID width heigth extension) 
+0

嗯..我喜歡陣列語法,但工程。謝謝 ! – nha

+1

是的,數組語法更好。這可能工作:'[「/my-route/:mongoID.:width:x:height.:extension」:x#「x」:width#「\ d +」:height#「\ d +」:mongoID#「[^\\。] +「]'。這有點麻煩:我們需要捕獲'x'作爲單獨的參數(解構時可以忽略),並且需要mongoID的正則表達式(而不是[default])(https://github.com/weavejester /clt/blob/master/src/clout/core.clj#L91)''[^ /,;?] +「'),所以它停在點 – nberger

+0

這也是一個不錯的選擇;非常感謝。我不能再贊成你了,但我會:) – nha