Map route in Compojure / Clout

I am trying to match routes of the following form: {{mongoID}}.{{width}}x{{height}}.{{extension}}

For example, /5591499e2dbc18bd0f000050.240x240.jpeg

is a valid route.

I would like to be able to destroy it like this:

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

      

Compojure

supports regex and dots seem to be https://github.com/weavejester/compojure/issues/42 too .

I can have individual regex for each of the fields, but I'm not sure how to put that in the route path (I'm trying to use the array syntax): https://github.com/weavejester/compojure/wiki/Routes-In-Detail# matching-the-uri

Let's say I have this:

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

      

Obviously the string "/my-route/:mongoID.:widthx:height.:extension"

won't work (just because the "x" is lost, maybe something else).

How do I change the route to match my arguments?

Note. I also use Prismatic / Schema if helpful.

+3


source to share


1 answer


Compojure uses clout for route matching. This allows you to specify a regular expression for each parameter. The following actions affect:

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"}

      



So, the following should work in compojure:

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

      

+4


source







All Articles