title: Serving app and api routes with different middleware using Ring and Compojure
body: <p>I have a ring+compojure application and I want to apply different middleware depending on whether the route is part of the web application or part of the api (which is json based). </p>
  
  <p>I found some answers to this question on stack overflow and other forums, but these answers seem more complicated than the solution I've been using. I wanted to know if there are drawbacks with how I'm doing it and what I may be missing in my solution.  A very simplified version of what I'm doing is</p>
  
  <pre><code>  (defroutes app-routes
      (GET "/" [req] dump-req)
      (route/not-found "Not Found"))
  
  (defroutes api-routes
    (GET "/api" [req] dump-req))
  
  (def app
    (routes (-&gt; api-routes
                (wrap-defaults api-defaults))
            (-&gt; app-routes
                (wrap-defaults site-defaults))))
  </code></pre>
  
  <p>Note that there is more middleware than I have shown here. </p>
  
  <p>The only 'restriction' I've encountered is that as the app-routes has the not-found route, it needs to come last or it will be triggered before finding the api routes. </p>
  
  <p>This seems simpler and more flexible than some of the other solutions I've found, which appear to either use additional conditional middleware, such as ring.middleware.conditional or what seems to me as more complex routing definitions where there is an additional defroutes layer and the need to define defroutes with ANY "*" etc. </p>
  
  <p>I suspect there is something subtle I'm missing here and while my approach seems to work, it will cause unexpected behaviour or results in some situations etc. </p>