ring

silian 2024-05-23T21:09:14.468829Z

I cannot seem to implement a simple counter in my session. I would like something as simple as :counter in my request (or perhaps it must be in :session) to increment on the execution of any route. The :counter is incremented once to 1 but increments no further on refresh or visit to any other route. I feel I am missing something very simple.

weavejester 2024-05-30T18:10:39.731789Z

It might be more intuitive to think of middleware like: `(defn wrap-identity [handler] (fn [request] (let [response (handler request)] response)))`

❤️ 1
weavejester 2024-05-30T18:11:25.769319Z

To update the session, you need to attach the updated session to the :session key on the response.

weavejester 2024-05-30T18:11:44.814959Z

Does that help make things clear?

silian 2024-05-30T18:46:44.788819Z

Your example is very clear. This also helped me, will leave here: https://gist.github.com/krisleech/893f396d864c75fde9e6933c9ac0cb09

silian 2024-06-11T06:53:40.917109Z

So, to update session, I would do something like:

(defn wrap-identity [handler]
  (fn [req]
    (let [resp (handler req)
          updated-session (assoc (-> req :session) :random-thing "whatever")]
      (assoc resp :session updated-session)))

silian 2024-05-23T21:13:26.777889Z

I believe I just rubber-ducked myself! (It really does work!) duckie Never mind, still stuck!

silian 2024-05-23T22:07:19.266139Z

Here is my update-counter fn:

(defn update-counter [handler]
  (fn [req]
    (handler
     (if (-> req :session :count)
       (let [count (-> req :session :count)
             updated-session (assoc (:session req) :count (inc count))]
         (-> req
             (assoc :session updated-session)
             (assoc :does-this-work? "yes"))) 
       (assoc req :crazy "town")))))

silian 2024-05-23T22:09:27.957199Z

And my app and its middleware:

(def real-app
  (-> (fn [req] (app req))
      wrap-anti-forgery 
      wrap-special
      update-counter
      wrap-session))

; my jetty server
(defn -main [& [port]]
  (let [port (Integer. (or port (env :port) 5010))]
    (jetty/run-jetty
     (fn [req] (real-app req))
     {:port port :join? false})))

2024-05-24T01:22:57.079289Z

Your update-counter middleware is updating the value on the way in, not on the way out

silian 2024-05-24T01:27:10.664349Z

I am using Compojure. I guess I don't understand where with middleware to modify or update the session in the response.

silian 2024-05-24T01:29:28.617909Z

If I want to apply my wrapper to all routes (just to keep it simple for now) but need to check current count on the request, don't I still wrap my real-app? I'm having trouble finding documentation that lays it out clearly.

2024-05-24T01:33:58.290759Z

You are updating the session value being passed to your handler, but not including an updated session in the response that flows back out of the handler, back out of your middleware and back to the session middleware

silian 2024-05-24T01:36:45.802409Z

(-> req (assoc :session updated-session) ?

silian 2024-05-24T01:38:02.317609Z

Isn't the above the modified response flowing out?

2024-05-24T01:38:31.081169Z

No

2024-05-24T01:38:55.536389Z

Reqs flow in resps flow out

silian 2024-05-24T01:40:43.292709Z

Where are responses passed in a Compojure route?

silian 2024-05-24T01:41:56.796989Z

Rather, I know the answer to that, but if I wanted to modify the responses for all routes -- I don't understand where the middleware for that goes.

2024-05-24T01:43:05.066539Z

same middleware does it

silian 2024-05-24T01:44:22.483779Z

Are you saying I need to pass something like res? I don't understand, I thought the output of these handles is the response, which is simply a request modified.

silian 2024-05-24T01:46:03.914859Z

If there is a resource worth reviewing to better understand the concept, I will appreciate any pointers. Thanks very much

2024-05-24T05:55:42.846659Z

a handler is a function that takes a request and returns a response

2024-05-24T05:55:56.436069Z

a middleware takes a handler and returns a handler

2024-05-24T05:56:13.149669Z

update-counter is a middle

2024-05-24T05:56:30.732669Z

inside the middleware is the new handler it defines

2024-05-24T05:57:59.206009Z

the new handler update-counter creates messes around with the session in the request before passing it to the original handler that was passed to update-counter and then returns the result of that original handler unchanged

2024-05-24T05:58:41.188469Z

you need to ensure that the session is set with the updated count is added to the result of the original handler before returning it

silian 2024-05-24T06:19:48.936109Z

Thank you for taking the time to explain. I will try again