On Jun 24, 2:45 am, Vagif Verdi <
Vagif.Ve...@gmail.com> wrote:
> I tried to change with-session myself, but hit the wall (i'm a clojure
> newbee):
We can probably leave with-session the way it is. I think these are
the two functions we need to change:
compojure.http.response/update-response
compojure.http.session/alter-session
Compojure uses the update-response method to update the HTTP response
with the return value from a route macro. The type of the return value
determines how the response is updated.
For instance, if the return value is a string, it is added to the body
and a default content type of "text/html" is set:
(GET "/" "Hello World")
=> (create-response request "Hello World")
=> (update-response request default-response "Hello World")
=> (update-response request {:status 200, :headers {}} "Hello
World")
=> {:status 200
:headers {"Content-Type" "text/html"}
:body "Hello World"}
You can also return a function from a route macro. When update-
response is called, it passes the request to the function and uses the
return value to update the response.
(GET "/" (fn [request] "Hi"))
=> (update-response request default-response (fn [request] "Hi"))
=> (update-response request default-response "Hi")
The advantage of this approach is that we can nest a Ring handler
within a route macro without requiring any special syntax:
(GET "/" (GET "/" "Nested!))
=> (GET "/" "Nested!")
However, the disadvantage is that we have no control over how the
response is updated. This is problematic for the alter-session
function:
(defn alter-session [func & args]
(fn [request]
(set-session
(apply func (request :session) args))))
Here alter-session uses a function to update the request session.
However, it doesn't know about the response, so this will overwrite
any existing :session key that has been put into the response.
I think we should probably pass both the session and the partially
generated response into the function. For example:
(GET "/" (fn [request response] "Hello World"))
This would mean we couldn't return a handler function directly anymore
(because handler functions only take one argument), but we could
always create a wrapper function like the one below for those cases:
(defn to-route [handler]
(fn [request response]
(handler request)))
The advantage of passing the response in is that we gain more control,
and that's part of what Compojure is about.
If you want to make this change, I can attempt to clarify of any part
of that explanation that's unclear. I really appreciate any effort
people make toward contributing to Compojure. But I also don't mind
making the change, if you happen to be short on time.
- James