Skip to content

Instantly share code, notes, and snippets.

@aboekhoff
Created January 12, 2010 02:41
Show Gist options
  • Save aboekhoff/274828 to your computer and use it in GitHub Desktop.
Save aboekhoff/274828 to your computer and use it in GitHub Desktop.
dynamic session middleware
(ns #^{:doc "A handful of utilities for handling sessions"}
somnium.financier.app.control.session
(:refer-clojure :exclude (assoc! dissoc! get)))
;;;; declaring session var here for global-ish access to session
(def *session* {})
(def *flash* {})
(defn with-dynamic-session
[handler]
(fn [{:keys [session flash] :as request}]
(binding [*session* session
*flash* flash]
(let [response (handler request)]
(assoc response :session *session*)))))
(defn clear!
"resets the session"
[]
(set! *session* nil))
(defn get
"get a key from the session"
([k] (*session* k))
([k else] (or (*session* k) else)))
(defn assoc!
"associates a value in the session"
[k v]
(set! *session* (assoc *session* k v)))
(defn flash-assoc!
[k v]
(set! *flash* (assoc *flash* k v)))
(defn dissoc!
"dissociates a value from the session"
[k]
(set! *session* (dissoc *session* k)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment