Skip to content

Instantly share code, notes, and snippets.

@roman
Created June 6, 2012 03:09
Show Gist options
  • Save roman/2879620 to your computer and use it in GitHub Desktop.
Save roman/2879620 to your computer and use it in GitHub Desktop.
ring usage example
(ns github
(:use [ring.middleware.cookies :only [wrap-cookies]]
[ring.middleware.session :only [wrap-session]]
[ring.middleware.lint :only [wrap-lint]]
[ring.middleware.session.cookie :only [cookie-store]]))
(defn index-handler [{:keys [session] :as req}]
(let [body (if (session :signed-in? false)
"welcome back!"
"howdy stranger")]
{ :status 200
:headers { "Content-Type" "text/plain" }
:body body }))
(defn sign-in-handler [{:keys [session] :as req}]
(let [session_ (assoc session :signed-in? true)]
{:status 200
:headers { "Content-Type" "text/plain" }
:body "I'll try to remember you"
:session session_ }))
(defn sign-out-handler [{:keys [session] :as req}]
(let [session_ (assoc session :signed-in? false)]
{:status 200
:headers { "Content-Type" "text/plain" }
:body "I'll try to forget you"
:session session_ }))
(defn router-app [{:keys [uri] :as req}]
(cond
(re-find #"^/$" uri) (index-handler req)
(re-find #"^/sign-in/?$" uri) (sign-in-handler req)
(re-find #"^/sign-out/?$" uri) (sign-out-handler req)
:else
(throw (Exception. "Invalid url"))))
(def app
(-> router-app
wrap-lint
(wrap-session { :store (cookie-store) })
wrap-cookies))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment