Fork me on GitHub
#ring
<
2017-02-08
>
weavejester16:02:22

@mgrbyte The problem is that you’re adding the middleware from scratch each time a request is being received.

weavejester16:02:22

@mgrbyte And because session storage is in-memory by default, you’re clearing the session store each time a request is received.

weavejester16:02:59

Instead of:

(defn my-handler [request]
  (let [handler (-> app-routes
                    (wrap-defaults site-defaults))]
    (handler request)))

weavejester16:02:30

You want something like:

(def my-handler
  (-> app-routes
      (wrap-defaults site-defaults)))

mgrbyte16:02:50

@weavejester how could I not see that! thank you! 🙂

mgrbyte16:02:06

In my "real" app, the reason for this is the design of my handlers

weavejester16:02:08

No problem. Sorry it took me so long to reply.

mgrbyte16:02:31

they all take a datomic db object that's got on each request, so using 'def' isn't an option with my current scheme

weavejester16:02:58

Have you considered using a closure instead?

mgrbyte16:02:00

need to find a way to inject the db val into each handler, or simply have each handler use (d/db conn) rather than passing it in

weavejester16:02:03

So something like:

weavejester16:02:17

(defn app-routes [conn]
  (routes
   (GET …)))

(defn make-handler [conn]
  (-> (app-routes conn)
      (wrap-defaults site-defaults)))

weavejester16:02:53

I tend to use the term “endpoint” to denote a function that returns a handler.

mgrbyte16:02:09

:thumbsup:

mgrbyte16:02:04

thanks again @weavejester , sometimes can't see wood from 🌳

owen20:02:00

hey I have a question regarding sessions, it seems like if I'm not specifically setting a session key the session/cookie (I'm using a cookie store) is cleared but if I do set it, the expiration of the cookie is reset every time, is both of these expected behavior?

owen20:02:41

I think I got confused thinking the wrap-session middleware with a cookie-store supports the same options as wrap-cookie (ie. max-age) but I think I still see behavior where if I don't set a key on a session it is nil even if it was set before, is that correct? also then do we have to manually expire sessions?

weavejester22:02:45

@owen the cookie sessions should expire when the cookies expire, or when the encryption key is changed.