Fork me on GitHub
#ring
<
2019-01-07
>
dbernal18:01:48

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

seancorfield18:01:08

@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 vs GET )

dbernal18:01:38

@seancorfield yes. As a header param

seancorfield18:01:24

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

seancorfield18:01:04

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)

seancorfield18:01:39

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

dbernal18:01:25

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

seancorfield19:01:44

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

seancorfield19:01:36

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

seancorfield19:01:11

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

ikitommi19:01:24

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] ...)}))

dbernal20:01:14

@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 @U055NJ5CC's example. If I was using the compojure api would it be something similar?

seancorfield20:01:34

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

dbernal20:01:59

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

ikitommi03:01:51

compojure-api sits on top of compojure-api, so same rules apply

seancorfield04:01:40

...on top of compojure… ?