clojurecompojurehttp-kit

How do I simplify Compojure routes?


I have the following code to define my routes in Compojure:

(ns my-project.my-test
  (:gen-class)
  (:require
   [my-test.template-views :refer :all]
   [compojure.core :refer [defroutes GET POST context]]
   [compojure.route :as route]
   [org.httpkit.server :refer [run-server]]))

(defn wrap-request
  [handler]
  (fn [request]
    (let [{remote-addr :remote-addr uri :uri scheme :scheme request-method :request-method} request]
      (println (str "REQUEST: " request)))
    (handler request)))

(defroutes app
  (wrap-request
   (GET "/" request
     {:status 200
      :headers {"Content-Type" "text/html"}
      :body (template-body (:uri request))}))
  (wrap-request
    (GET "/page1" request
      {:status 200
       :headers {"Content-Type" "text/html"}
       :body (template-body (:uri request))}))
  (wrap-request
    (GET "/page2" request
      {:status 200
       :headers {"Content-Type" "text/html"}
       :body (template-body (:uri request))}))
  (wrap-request
    (GET "/page3" request
      {:status 200
       :headers {"Content-Type" "text/html"}
       :body (template-body (:uri request))}))
  (route/resources "/")
  (route/not-found {:status 404
                    :headers {"Content-Type" "text/html"}
                    :body "<h1>Not Found</h1>"}))

That works but it seems like I should be able to simplify it like this:

(ns my-project.my-test
  (:gen-class)
  (:require
   [my-test.template-views :refer :all]
   [compojure.core :refer [defroutes GET POST context]]
   [compojure.route :as route]
   [org.httpkit.server :refer [run-server]]))

(defn wrap-request
  [handler]
  (fn [request]
    (let [{remote-addr :remote-addr uri :uri scheme :scheme request-method :request-method} request]
      (println (str "REQUEST: " request)))
    (handler request)))

(defn wrap-template
  [route]
  (wrap-request
   (GET route request
     {:status 200
      :headers {"Content-Type" "text/html"}
      :body (template-body (:uri request))})))

(defroutes app
  (map wrap-template ["/" "/page1" "/page2" "/page3"])
  (route/resources "/")
  (route/not-found {:status 404
                    :headers {"Content-Type" "text/html"}
                    :body "<h1>Not Found</h1>"}))

However, when I do, I get this error backtrace:

Sat Apr 24 22:38:33 MDT 2021 [worker-2] ERROR - GET /page2
java.lang.ClassCastException: class clojure.lang.LazySeq cannot be cast to class clojure.lang.IFn (clojure.lang.LazySeq and clojure.lang.IFn are in unnamed module of loader 'app')
    at compojure.core$routing$fn__368163.invoke(core.clj:185)
    at clojure.core$some.invokeStatic(core.clj:2705)
    at clojure.core$some.invoke(core.clj:2696)
    at compojure.core$routing.invokeStatic(core.clj:185)
    at compojure.core$routing.doInvoke(core.clj:182)
    at clojure.lang.RestFn.applyTo(RestFn.java:139)
    at clojure.core$apply.invokeStatic(core.clj:669)
    at clojure.core$apply.invoke(core.clj:662)
    at compojure.core$routes$fn__368167.invoke(core.clj:192)
    at org.httpkit.server.HttpHandler.run(RingHandler.java:117)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

Is there something about using (map) that is wrong here?


Solution

  • routes (and thus defroutes) expects each argument to be a request handler function. A list of handlers is not a handler function; hence the error. Happily, there is a function to convert a list of handlers to a single handler: routes! Since it wants N separate arguments, rather than a single list, you will need apply as well. So:

    (defroutes app
      (apply routes (map wrap-template ["/" "/page1" "/page2" "/page3"]))
      (route/resources "/")
      (route/not-found {:status 404
                        :headers {"Content-Type" "text/html"}
                        :body "<h1>Not Found</h1>"}))
    

    As an aside, I generally suggest not using defroutes, simply because it does not compose as easily as separate def + routes, and for beginners it leads to forgetting that anything but defroutes exists, when in fact most interesting servers will want to apply a function to some of their routes.