Somebody explain me the following order of calls in compojure

78 views
Skip to first unread message

Aravindh S

unread,
Dec 10, 2014, 9:43:18 AM12/10/14
to comp...@googlegroups.com
Hi, I am getting started with clojure and compojure. I have the following question.
Consider the following code:

Handler.clj file:

(defn middleware1 [handler]
(println "middleware1")
(fn [request]
(println "inside 1")
(let [response (handler request)]
(println "called response in 1")
response)))

(defn middleware2 [handler]
(println "middleware2")
(fn [request]
(println "inside 2")
(let [response (handler request)]
(println "called response in 2")
response)))

(def app
  (-> (routes home-routes app-routes)
      (handler/site)
 (middleware1)
 (middleware2)
      (wrap-base-url)))

Home.clj file:

(defn home []
(println "inside GET call")
  (layout/common [:h1 "Hello World!"]))

(defroutes home-routes
  (GET "/" [] (home)))

I have defined two middlewares and have added println to trace the calling order. When I do a get request, the following is the output:
middleware1
middleware2
inside 2
inside 1
inside GET call
called response in 1
called response in 2
inside 2
inside 1
called response in 1
called response in 2

Why there are 2 calls for each middleware?
Also since each middleware gets the response by calling (handler request), does that mean the actual GET is performed twice? I know that is not possible. Is there any memoization done?

Thanks
Aravindh.S

James Reeves

unread,
Dec 10, 2014, 9:48:02 AM12/10/14
to Compojure
What's the definition of `app-routes`?

- James

--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compojure+...@googlegroups.com.
To post to this group, send email to comp...@googlegroups.com.
Visit this group at http://groups.google.com/group/compojure.
For more options, visit https://groups.google.com/d/optout.

Aravindh S

unread,
Dec 10, 2014, 10:18:13 AM12/10/14
to comp...@googlegroups.com, ja...@booleanknot.com
(defroutes app-routes
  (route/resources "/")
  (route/not-found "Not Found"))

James Reeves

unread,
Dec 10, 2014, 10:44:53 AM12/10/14
to Compojure
Well, just running the app and hitting it with Curl doesn't show up the additional middleware calls.

My guess is that you're accessing it with a browser, right? So that's going to make an implicit call to /favicon.ico. That's why the middleware is activated twice, because your browser is making two requests.

- James

Aravindh S

unread,
Dec 10, 2014, 10:49:25 AM12/10/14
to comp...@googlegroups.com

Hi James, yes I was making a call from browser. Your reply makes sense. But my other question is since every middleware gets the response by calling  (handler response), does this mean that the actual handler is invoked twice?

You received this message because you are subscribed to a topic in the Google Groups "Compojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/compojure/sMvQOIkOAVs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to compojure+...@googlegroups.com.

James Reeves

unread,
Dec 10, 2014, 11:14:45 AM12/10/14
to Compojure
Yes, each time you send a request to the adapter, the adapter calls the handler in order to get a response. So if there are two requests, the handler will be called twice.

- James

Aravindh S

unread,
Dec 10, 2014, 11:17:13 AM12/10/14
to comp...@googlegroups.com

Hi James, that's fine. But what is there are more than one middle ware, where each call the handler for the same request? In that case will there be two calls to handler?

James Reeves

unread,
Dec 10, 2014, 11:24:01 AM12/10/14
to Compojure
No, the handler is only called once per request, at least for the code you've written.

- James
Reply all
Reply to author
Forward
0 new messages