Created
July 20, 2011 20:24
-
-
Save toymachine/1095841 to your computer and use it in GitHub Desktop.
Ring middleware adding session support to app-engine-magic based GAE apps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;ring middleware for use with app-engine magic. this middleware provides the ring session interface (session as map in request), | |
;but implemented on top of the session implementation provided by google app-engine | |
;you need '<sessions-enabled>true</sessions-enabled>' in your war/WEB-INF/appengine-web.xml file to enable the session support in app-engine | |
(defn wrap-ae-session [app] | |
(fn [req] | |
(let [http-session (.getSession (req :request)) | |
;build-up ring like session map from httpservlet | |
names-in (enumeration-seq (.getAttributeNames http-session)) | |
session-in (into {} (for [name names-in] [name (.getAttribute http-session name)]))] | |
;pass the session map in the request | |
(let [resp (app (assoc req :session session-in))] | |
;check for :session in response map to process changes to session | |
(if (contains? resp :session) | |
;changes to session present | |
(if (resp :session) | |
;non-nil session, add/change or delete | |
(let [session-out (resp :session)] | |
;check for updates or removed | |
(doseq [name names-in] | |
(if (contains? session-out name) | |
(when-not (= (session-in name) (session-out name)) | |
;update value | |
(.setAttribute http-session name (session-out name))) | |
;no longer there, there delete it | |
(.removeAttribute http-session name))) | |
;check for additions | |
(doseq [name (keys session-out)] | |
(when-not (contains? session-in name) | |
(.setAttribute http-session name (session-out name)))) | |
(dissoc resp :session)) | |
;nil session, delete all | |
(do | |
(.invalidate http-session) | |
(dissoc resp :session))) | |
;no session changes | |
resp))))) | |
;ring app (this will be different for your application) | |
(def app (-> main-routes | |
(wrap-ae-session))) | |
;app-engine magic | |
(ae/def-appengine-app magic-app (var app)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment