Fork me on GitHub

Hi, In my use case, I need to have different middle-ware for specific routes. This is the code:

(ns my-api.handler
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]))

(defn echo0[handler]
  (fn [request]
    (prn "ECHO0" #_request)
    (handler request)

(defn echo1[handler]
  (fn [request]
    (prn "ECHO1" #_request)
    (handler request)

(def app
   (middleware [echo0] (swagger-routes))
   (context "/api" []
            :middleware [echo1]
            (GET "/test" [] (ok "1")))))
However, when I run the code echo0 is invoked for /api/test as well. Is there is any way to make those completely separate?


@happy.lisper good question. The middleware is removed in 2.0.0 in favour of route-middleware, which should do what you want. The root cause here is that in Compojure, the routing tree is an application where the middleware are applied in-place - there is no good way to delay the computation to see if the route will eventually match.


@ikitommi ITY. I will try to play with route-middleware. In meantime, I just do the brute force filtering on :uri in a middleware applied to all routes.