ring

dbernal 2019-01-07T18:28:48.001300Z

anyone know if it's possible to version routes using ring and compojure?

seancorfield 2019-01-07T18:34:08.003200Z

@dbernal Are you thinking about passing API version in query string / form data, rather than embed the version in the URI itself? (i.e., GET <https://api.acme/data?version=2> vs GET <https://api.acme/v2/data> )

dbernal 2019-01-07T18:35:38.003800Z

@seancorfield yes. As a header param

seancorfield 2019-01-07T18:41:24.004400Z

You'd have to write some middleware for that, but it would be fairly straightforward.

seancorfield 2019-01-07T18:43:04.006Z

If you had your Compojure routes set up grouped under "/v1" and "/v2" then you could have middleware that looked for the header and updated the :uri field in the Ring Request (before the Compojure middleware gets control)

seancorfield 2019-01-07T18:43:39.006600Z

Assuming your routes are different for the different versions...

dbernal 2019-01-07T18:58:25.007400Z

gotcha. So the only way to do it would be to have separate compojure routes?

seancorfield 2019-01-07T19:32:44.007600Z

If the routes for V1 and V2 are different, you still have to define both.

seancorfield 2019-01-07T19:33:36.007800Z

If the routes are identical for V1 and V2, then just check the version inside your handler(s).

seancorfield 2019-01-07T19:34:11.008Z

(I'm puzzled by your response so perhaps I'm misunderstanding your original question)

ikitommi 2019-01-07T19:55:24.008200Z

I would do something like:

(defn on-version [versions]
  (fn [request]
    (let [version (get-in request [:headers "x-version"])
          handler (get versions version (constantly nil))]
      (handler request))))

(GET "/ping" []
  (on-version
    {"v1" (fn [req] ...)
     "v2" (fn [req] ...)}))

dbernal 2019-01-07T20:37:14.008600Z

@seancorfield sorry. I misunderstood. I thought you meant that if they were the same route then I would need to look at the headers and dispatch it to another compojure route but it seems like it just needs to be dispatched to a certain handler. I can kind of see that from @ikitommi's example. If I was using the compojure api would it be something similar?

seancorfield 2019-01-07T20:39:34.008800Z

compojure-api instead of compojure? I don't know, I've never used the former. I've only ever used the latter.

dbernal 2019-01-07T20:45:59.009Z

ah, I see. ok, I'll dig around a little bit more