Search code examples
google-app-engineclojurecompojureringappengine-magic

Simple example of Sandbar or Ring sessions in Google App Engine


I'm trying to work out how to get sessions and flash working in Google App Engine. Could someone provide a clear example using either Ring or Sandbar? I think I have sandbar working, specifically it doesn't tell me that Var sandbar.stateful-session/sandbar-flash is unbound and when I dump the handler I get :flash and :session though I'm not certain if that is a sandbar session or a ring one. For completeness I will mention that I am using the latest versions of appengine-magic, ring, hiccup and sandbar. There do not appear to be any incompatibilities or issues.

So a clear example preferably with use of flash-put!, flash-get, session-put! and session-get.


Solution

  • I don't usually like answering my own questions, however in this case I'll make an exception because:

    a) There isn't a lot of easy to understand examples out there.

    b) It would be nice to have a quick working example for others to use.

    Note: appengine-magic is not required here, this will also work with normal ring sessions

    Code

    ;; Place in a file called session.clj in the example project
    (ns example.session
        "Works with the normal ring sessions 
         allowing you to use side-effects to manage them")
    
    (declare current-session)
    
    (defn wrap-session! [handler]
      (fn [request]
        (binding [current-session (atom (or (:session request) {}))]
          (let [response (handler request)]
            (assoc response :session @current-session)))))
    
    (defn session-get
      ([k] (session-get k nil))
      ([k default] (if (vector? k)
                     (get-in @current-session k)
                     (get @current-session k default))))
    
    (defn session-put!
      ([m]
         (swap! current-session (fn [a b] (merge a m)) m))
      ([k v]
         (swap! current-session (fn [a b] (merge a {k b})) v)))
    
    (defn session-pop! [k]
      (let [res (get @current-session k)]
        (swap! current-session (fn [a b] (dissoc a b)) k)
        res))
    
    (defn session-delete-key! [k]
      (swap! current-session (fn [a b] (dissoc a b)) k))
    
    (defn session-destroy! []
      (swap! current-session (constantly nil)))
    
    
    ;; Place in a file called core.clj in the example project
    (ns example.core
      (:use compojure.core
            [ring.middleware.keyword-params :only [wrap-keyword-params]]
            [ring.middleware.session :only [wrap-session]]
            [ring.middleware.session.cookie :only [cookie-store]]
            [example session])
      (:require [appengine-magic.core :as ae]))
    
    (declare current-session)
    
    (defroutes example-app-routes
      (GET "/" _
           (fn [req]
             (let [counter (session-get :counter 0)]
               {:status 200
                :headers {"Content-Type" "text/plain"}
                :body (str "started: " counter)})))
      (GET "/inc" _
           (fn [req]
             (let [counter (do 
                            (session-put! :counter (inc (session-get :counter 0)))
                            (session-get :counter))]
               {:status 200
                :headers {"Content-Type" "text/plain"}
                :body (str "incremented: " counter)}))))
    
    (def example-app-handler
      (-> #'example-app-routes
          wrap-keyword-params
          wrap-session!
          (wrap-session {:cookie-name "example-app-session"
                         :store (cookie-store)})))
    
    (ae/def-appengine-app example-app #'example-app-handler)
    

    How to use it

    Navigating to http://127.0.0.1:8080/inc increments the counter in the session and http://127.0.0.1:8080/ will display the value of counter in the session.

    wrap-session! is not required for sessions to work, just using

    (wrap-session {:cookie-name "example-app-session"
                         :store (cookie-store)})
    

    will give you working functional sessions. However I wanted to manage my sessions with side-effects and wrap-session! provides that functionality. To use flash like functionality, simply use session-put! to put a value into the session and then use session-pop! to remove it.

    Hope that's helpful.