Search code examples
clojurecompojure

Getting the http request when using compojure.route/not-found in Clojure


I'm building a server using Cloujre's Compojure. The default route is compojure.route/not-found, is there a way of getting the request that reached this route? I'd like print all requests that end up there.


Solution

  • You can use this kinda approach:

    (def handler (-> your-routes
                     wrap-my-request-middleware ;; it has to be in this order
                     ...))
    

    Let's log in here the uri

    (defn wrap-my-request-middleware
      [handler]
      (fn [request]
        (let [response   (handler request)]
          (when (= 404 (:status response))
            ;; do whatever you like in here
            (log/info (str "Request path: " (:uri request))))
          response)));; fn needs to return reponse...