How to use sessions using lib-noir in Compojure

I think I have a pretty simple problem. But I have been looking at this screen for too long. So I try (and fail) to get stateful sessions in Compojure. The code is inserted here .

You can see me trying to use lib-noir (line 62) to initialize stateful sessions. Then, when the application is running, I try to make a session / put call ! some data in the session (line 43).

Now this stacktrace says in session.put !, lib-noir is trying to replace a var session that was not connected. Now I thought I did it on line 62. So I'm sure this is a simple fix that will see a different set of eyes.

  java.lang.ClassCastException: clojure.lang.Var $ Unbound cannot be cast to clojure.lang.Atom                                                                                                                                                                                                                                                                                                                                
         at clojure.core $ swap_BANG_.invoke (core.clj: 2110)                                                                                                                                                                                                                                                                                                                                                                  
         at noir.session $ put_BANG_.invoke (session.clj: 18)                                                                                                                                                                                                                                                                                                                                                                  
         at bkell.http.handler $ fn__6159.invoke (handler.clj: 156)                                                                                                                                                                                                                                                                                                                                                            
         at compojure.core $ make_route $ fn__3800.invoke (core.clj: 93)                                                                                                                                                                                                                                                                                                                                                         
         at compojure.core $ if_route $ fn__3784.invoke (core.clj: 39)                                                                                                                                                                                                                                                                                                                                                           
         at compojure.core $ if_method $ fn__3777.invoke (core.clj: 24)                                                                                                                                                                                                                                                                                                                                                          
         at compojure.core $ routing $ fn__3806.invoke (core.clj: 106)                                                                                                                                                                                                                                                                                                                                                           
         at clojure.core $ some.invoke (core.clj: 2390)                                                                                                                                                                                                                                                                                                                                                                        
         at compojure.core $ routing.doInvoke (core.clj: 106)                                                                                                                                                                                                                                                                                                                                                                  
         at clojure.lang.RestFn.applyTo (RestFn.java:139)                                                                                                                                                                                                                                                                                                                                                                   
         ...
         at java.lang.Thread.run (Thread.java:619)

thanks

+6
source share
3 answers

James Reeves answered at - https://groups.google.com/forum/#!topic/compojure/yG6nCXiEinU

Try exchanging links with the handler / site and wrap-noir-session *. Because the handler / site uses the wrap-session middleware and wrap-noir-session * middleware, depending on the documentation, the handler / site should be used before the -noir-session ends.

  • James
+2
source

I think you want wrap-noir-session , not wrap-noir-session* . The documentation for wrap-noir-session* says (my emphasis) "The state layer around wrap-session. Expects that wrap-session has already been used. "

If you want to use wrap-noir-session* , I think you need to use wrap-session explicitly.

0
source

Putting my answer, I continue to come to this question and forget that it has been resolved.

 (ns my-project.handler (:require (compojure [handler :as handler] [route :as route] [core :refer :all]) [noir.util.middleware :as middleware] [noir.session :as session])) (defroutes my-routes (GET "/put" req (session/put! :test "yes")) (GET "/get" req (session/get :test)) (route/resources "/") (route/not-found "Not Found")) (def app (-> (handler/site my-routes) session/wrap-noir-flash ; only if you need flash-put! and flash-get session/wrap-noir-session)) 
0
source

All Articles