Re: sessions in Ring, Sandbar, Compojure, etc...

2011-07-26 Thread Shree Mulay
Dear Brenton, James, et al.,

The problem what the (wrap-reload) function that I was using in my code, 
which isn't necessary anymore as ring, by default, includes 
the functionality now (I think???). 
http://mmcgrana.github.com/ring/middleware.reload-api.html

Brenton, I was wondering if there's an sandbar api page anywhere? I've 
looked, but couldn't find.

Do let me know, eh

Thanks,

shree

On Wednesday, May 11, 2011 10:34:45 PM UTC-4, Shree Mulay wrote:
>
> Thanks Brenton, 
>
> I've gone through your example, and tried to replicate it at my 
> end(for my use cases) but was unable to. I demoed most of the examples 
> you've provided and they look GREAT! I hope that I'll be able to make 
> projects work (and look) like the examples you've shared with us one 
> day. 
>
> The problem with: 
>
> https://github.com/brentonashworth/sandbar/blob/master/src/examples/sessions/session_with_reload.clj
>  
>
> -and-
>  
> https://gist.github.com/608048 
>
> is they deal with sessions on a single page that is reloaded, rather 
> than the session getting transfered from one page to another, via GET, 
> POST, PUT, ALL, etc, in defroutes.  Um, moreso, using hiccup's 
> redirect, eh 
>
> Any thoughts, would be appreciated! 
>
> Thanks again, 
>
> shree 
>
>
> On May 11, 9:48 pm, Brenton  wrote: 
> > Shree, 
> > 
> > I would suggest that you try to use only Ring first. Once you get how 
> > it works, take a look at Sandbar and see if that may work better for 
> > you in specific situations. 
> > 
> > The session code in Sandbar is stable. It will not change much in the 
> > next release. In fact it may become its own project by then. 
> > 
> > Here is one small application that shows how to use both styles in one 
> > app. I hope you find this helpful. 
> > 
> > http://github.com/brentonashworth/sandbar/blob/master/src/examples/se... 
>
> > 
> > Brenton 
> > 
> > On May 9, 11:24 pm, Shree Mulay  wrote: 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > > For the life of me, I can't get sessions to work, immaterial of which 
> > > tutorial I try and get going???  Is there any tutorial out there that 
> > > explicitly explains everything for a newb like me? After several 
> > > round, I did successfully get form params to work! YEAH! But now, I'd 
> > > like to create a login so that the user can login and have state - but 
> > > I can't get SESSIONS TO WORK??? 
> > 
> > > frustrated!, 
> > 
> > > shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: sessions in Ring, Sandbar, Compojure, etc...

2011-07-26 Thread Shree Mulay
On Saturday, May 14, 2011 4:48:03 PM UTC-4, James Reeves wrote:
>
> On 14 May 2011 20:49, Shree Mulay  wrote:
> > One final thought I had is I've noticed if I reload a page, the
> > session information is lost. How do we get around this?
>
> You could use defonce to define an atom to back the session storage. e.g.
>
>   (require 'ring.middleware.session.memory)
>
>   (defonce memory (atom {}))
>
>   (def app
> (-> main-routes
>   (wrap-session {:store (memory-store memory)}))
>
> Perhaps session memory-stores need to use defonce by default.
>
> - James
>

Dear James, et al.,

Since I updated to ring 0.4.5, I noticed that I didn't have the session 
disappearing when I reloaded the page. Does this mean that I don't need to 
back up the session state anymore? Or what has changed?

Thanks,

shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-14 Thread James Reeves
On 14 May 2011 20:49, Shree Mulay  wrote:
> One final thought I had is I've noticed if I reload a page, the
> session information is lost. How do we get around this?

You could use defonce to define an atom to back the session storage. e.g.

  (require 'ring.middleware.session.memory)

  (defonce memory (atom {}))

  (def app
(-> main-routes
  (wrap-session {:store (memory-store memory)}))

Perhaps session memory-stores need to use defonce by default.

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-14 Thread Shree Mulay
On May 12, 8:12 pm, James Reeves  wrote:
> On 13 May 2011 00:23, Shree Mulay  wrote:
>
> > I didn't know there was? What I understood of wrap-reload is that I
> > didn't have to restart "lein ring server" at the command line
> > everytime I made a change to my core.clj file???
>
> > How do I set up lein-ring to give me the same functionality?
>
> lein-ring will automatically reload any modified file in your src
> directory without you having to do a thing.
>
> Try starting "lein ring server" and then changing one of your source
> files. When you refresh the page in your browser, the changes should
> instantly be visible without the need for a restart.
>
> - James

James, et al.,

I later discovered that this functionality became built into ring
server! :) I appreciate all the help that you(and others) have given
me. I've been able to move forward thanks to this.

One final thought I had is I've noticed if I reload a page, the
session information is lost. How do we get around this?

Thanks,

shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-12 Thread James Reeves
On 13 May 2011 00:23, Shree Mulay  wrote:
> I didn't know there was? What I understood of wrap-reload is that I
> didn't have to restart "lein ring server" at the command line
> everytime I made a change to my core.clj file???
>
> How do I set up lein-ring to give me the same functionality?

lein-ring will automatically reload any modified file in your src
directory without you having to do a thing.

Try starting "lein ring server" and then changing one of your source
files. When you refresh the page in your browser, the changes should
instantly be visible without the need for a restart.

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-12 Thread Shree Mulay
James, et al.,

> I'm not sure. There exist better alternatives to the `wrap-reload`
> middleware anyway, such as the lein-ring plugin.
>

I didn't know there was? What I understood of wrap-reload is that I
didn't have to restart "lein ring server" at the command line
everytime I made a change to my core.clj file???

How do I set up lein-ring to give me the same functionality?

Cause, I'd REALLY appreciate that!

Appears I'd save a LOT of time! It's painful having to restart jvm
everytime I intend to make a change to the html, eh

>
> Yes, because "x" is a parameter that is never set. In my original example:
>
>    (GET "/set" [x :as {session :session}]
>      {:session (assoc session :x x)
>      :body (str "You set x = " x)})
>
> So you'd set it by going to "http://localhost:3000/set?x=foo";.
>
> You then hard-coded the :x session key to "Shree", but you didn't
> change the value of the "x" variable:
>
>    (GET "/set" [x :as {session :session}]
>      {:session (assoc session :x "Shree")
>      :body (str "You set x = " x)})

Gosh, I feel STUPID!

Thanks everyone for their patience and their time! I'm on my way!

Regards,

shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-12 Thread James Reeves
On 12 May 2011 20:37, Shree Mulay  wrote:
>  ;(wrap-reload '(SocialNetworkCore.core))  ;;remove wrap-reload
>
> it started behaving. Is the "wrap-reload" functionality not compatible
> with Ring Sessions??? Have I implemented it the wrong way?

I'm not sure. There exist better alternatives to the `wrap-reload`
middleware anyway, such as the lein-ring plugin.

> Only thing is, when I goto
>
> http://127.0.0.1:3000/set
> I still get:
> You set x =
>
> Though at:
>
> http://127.0.0.1:3000/get
> I get:
> x = Shree

Yes, because "x" is a parameter that is never set. In my original example:

   (GET "/set" [x :as {session :session}]
 {:session (assoc session :x x)
 :body (str "You set x = " x)})

So you'd set it by going to "http://localhost:3000/set?x=foo";.

You then hard-coded the :x session key to "Shree", but you didn't
change the value of the "x" variable:

   (GET "/set" [x :as {session :session}]
 {:session (assoc session :x "Shree")
 :body (str "You set x = " x)})

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-12 Thread Shree Mulay
James, et al.,

WHOA! I got it to work!

After I commented out

  ;(wrap-reload '(SocialNetworkCore.core))  ;;remove wrap-reload

it started behaving. Is the "wrap-reload" functionality not compatible
with Ring Sessions??? Have I implemented it the wrong way?

Only thing is, when I goto

http://127.0.0.1:3000/set
I still get:
You set x =

Though at:

http://127.0.0.1:3000/get
I get:
x = Shree

All thoughts are appreciated. Thanks! Y'all's help has been
wonderful! :)

shree

On May 12, 2:57 pm, Shree Mulay  wrote:
> @ James, yes.
>
> (def app
>     (-> #'main-routes
>         (wrap-reload '(SocialNetworkCore.core))  ;;remove wrap-reload
> from production code
>         (wrap-params)               ;; wraps params (inputs from
> forms) - makes 'em available
>         ;(wrap-flash)                ;;
>         (wrap-session)              ;; wraps session
>         (wrap-request-logging)      ;; gives me request logging
>         (wrap-bounce-favicon)       ;; skirts favicon request logging
>         (wrap-exception-logging)    ;; logs exceptions
>         (wrap-stacktrace)))         ;; detailed stacktrace of errors
>
> Anything look wrong there???
>
> On May 12, 7:02 am, James Reeves  wrote:
>
>
>
>
>
>
>
> > On 12 May 2011 04:49, Shree Mulay  wrote:
>
> > > But for WHATEVER reason, the sessions are NOT getting stored?
>
> > Have you added the session middleware? e.g.
>
> > (def app
> >   (-> main-routes
> >       wrap-params
> >       wrap-session))
>
> > Or more succinctly:
>
> > (def app
> >   (handler/site main-routes))
>
> > - James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-12 Thread Shree Mulay
@ James, yes.

(def app
(-> #'main-routes
(wrap-reload '(SocialNetworkCore.core))  ;;remove wrap-reload
from production code
(wrap-params)   ;; wraps params (inputs from
forms) - makes 'em available
;(wrap-flash);;
(wrap-session)  ;; wraps session
(wrap-request-logging)  ;; gives me request logging
(wrap-bounce-favicon)   ;; skirts favicon request logging
(wrap-exception-logging);; logs exceptions
(wrap-stacktrace))) ;; detailed stacktrace of errors

Anything look wrong there???

On May 12, 7:02 am, James Reeves  wrote:
> On 12 May 2011 04:49, Shree Mulay  wrote:
>
> > But for WHATEVER reason, the sessions are NOT getting stored?
>
> Have you added the session middleware? e.g.
>
> (def app
>   (-> main-routes
>       wrap-params
>       wrap-session))
>
> Or more succinctly:
>
> (def app
>   (handler/site main-routes))
>
> - James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-12 Thread James Reeves
On 12 May 2011 04:49, Shree Mulay  wrote:
> But for WHATEVER reason, the sessions are NOT getting stored?

Have you added the session middleware? e.g.

(def app
  (-> main-routes
  wrap-params
  wrap-session))

Or more succinctly:

(def app
  (handler/site main-routes))

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
James,

I even tried the examples you gave me:

(defroutes main-routes
(GET "/" [] (welcome))
(GET "/form" [] (render-form))
(GET "/login/" [] (login-view))
;   (POST "/login/" {request :request params :params} (login-
controller request params))
(POST "/login/" {params :params session :session} (login-
controller params session))
;   (ANY "/logout/" [] (logout-controller session))
(ANY "/page/" {request :request session :session} (page request
session "hello" "world"))
(GET "/a" [] (ooops))
(GET "/set" [x :as {session :session}]
  {:session (assoc session :x "Shree")
  :body (str "You set x = " x)})
(GET "/get" {session :session}
  (str "x = " (:x session)))
(POST "/form" {params :params} (handle-form params))
(route/not-found (page_not_found)))

But for WHATEVER reason, the sessions are NOT getting stored? When I
goto the set page, I get:

http://localhost:3000/set
  You set x =

-and-

http://localhost:3000/get
  x =

respectively. I must REALLY be messing SOMETHING up at my end???  Why
else would this NOT be working???

Thanks,

shree


On May 11, 8:01 pm, James Reeves  wrote:
> On 12 May 2011 00:04, Shree Mulay  wrote:
>
> > From this code, I can't figure out where you "instantiate" the session
> > var, store to it, and read back from it? I know there's not
> > instantiation in clojure(???). I hope your able to get what I'm having
> > trouble figuring out.
>
> The session is read from a :session key on the request map, and
> written using a :session key on the response map.
>
> For example, this route returns the value of the session key :x
>
>   (GET "/get" {session :session}
>     (str "x = " (:x session)))
>
> It's equivalent to the Sinatra route:
>
>   get "/get" do
>     "x = #{session[:x]}"
>   end
>
> The main difference is that the session has to be bound explicitly. We
> don't just get a magic session map hanging around in the background.
>
> Writing a session is a little trickier:
>
>   (GET "/set" [x :as {session :session}]
>     {:session (assoc session :x x)
>      :body (str "You set x = " x)})
>
> The equivalent Sinatra route is:
>
>   get "/set" do |x|
>     session[:x] = x
>     "You set x = #{x}"
>   end
>
> Ring sessions are a little unwieldy because you write by changing the
> response, rather than calling a side-effectful function. Sandbar takes
> a slightly different approach that looks more like the Sinatra code:
>
>   (GET "/get" []
>     (str "x = " (session-get :x)))
>
>   (GET "/set" [x]
>     (session-put! :x x)
>     (str "You set x = " x))
>
> Sandbar is a little cleaner for simple things, but Ring sessions have
> the advantage of working well with middleware. Idiomatic Ring tends to
> use a lot of middleware, far more than one might use in Rack (for
> instance).
>
> - James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
Aaron, et al.,

That didn't seem to make any difference. I believe the example I got
"dosync" from wanted the whole thing to move forward as a transaction
(???). Anyhow, this is what it looks like now:

(defn login-controller [params session]
  (do (println (params "password") (params "name")))
  (dosync
(if
  (= "secret" (params "password"))
; Username can include letters, numbers,
; spaces, underscores, and hyphens.
;(.matches (params :name) "[\\w\\s\\-]+"))
  (do
(assoc (redirect "/page/") :session (assoc session :name (params
"name")))
;{:session (assoc session :name (params "name"))}
;(redirect "/page/"))
)
  (redirect "/oops/"

my repl(output) still gets me:

"Getting userid: nil"

Any thoughts?

Thanks,

shree

On May 11, 10:54 pm, Aaron Cohen  wrote:
> On Wed, May 11, 2011 at 10:27 PM, Shree Mulay  wrote:
>
> > ;;takes the input params from the login form and stores the
> > ;;logged in user into a session. You get logged in when the
> > ;;password value is = secret
> > (defn login-controller [params session]
> >  (do (println (params "password") (params "name")))
> >  (dosync
>
> What is this dosync for?
>
> >    (if
> >      (= "secret" (params "password"))
> >        ; Username can include letters, numbers,
> >        ; spaces, underscores, and hyphens.
> >        ;(.matches (params :name) "[\\w\\s\\-]+"))
> >      (do
> >        {:session (assoc session :name (params "name"))}
>
> This line accomplishes nothing, it creates a map and then throws it away.
>
> Try returning something like:
> (assoc (redirect "/page/") :session (assoc session :name (params "name")))
>
>
>
>
>
>
>
> >        (redirect "/page/"))
> >      (redirect "/oops/"

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Aaron Cohen
On Wed, May 11, 2011 at 10:27 PM, Shree Mulay  wrote:
>
> ;;takes the input params from the login form and stores the
> ;;logged in user into a session. You get logged in when the
> ;;password value is = secret
> (defn login-controller [params session]
>  (do (println (params "password") (params "name")))
>  (dosync

What is this dosync for?

>    (if
>      (= "secret" (params "password"))
>        ; Username can include letters, numbers,
>        ; spaces, underscores, and hyphens.
>        ;(.matches (params :name) "[\\w\\s\\-]+"))
>      (do
>        {:session (assoc session :name (params "name"))}

This line accomplishes nothing, it creates a map and then throws it away.

Try returning something like:
(assoc (redirect "/page/") :session (assoc session :name (params "name")))

>        (redirect "/page/"))
>      (redirect "/oops/"

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
Thanks Brenton,

I've gone through your example, and tried to replicate it at my
end(for my use cases) but was unable to. I demoed most of the examples
you've provided and they look GREAT! I hope that I'll be able to make
projects work (and look) like the examples you've shared with us one
day.

The problem with:

https://github.com/brentonashworth/sandbar/blob/master/src/examples/sessions/session_with_reload.clj
-and-
https://gist.github.com/608048

is they deal with sessions on a single page that is reloaded, rather
than the session getting transfered from one page to another, via GET,
POST, PUT, ALL, etc, in defroutes.  Um, moreso, using hiccup's
redirect, eh

Any thoughts, would be appreciated!

Thanks again,

shree


On May 11, 9:48 pm, Brenton  wrote:
> Shree,
>
> I would suggest that you try to use only Ring first. Once you get how
> it works, take a look at Sandbar and see if that may work better for
> you in specific situations.
>
> The session code in Sandbar is stable. It will not change much in the
> next release. In fact it may become its own project by then.
>
> Here is one small application that shows how to use both styles in one
> app. I hope you find this helpful.
>
> http://github.com/brentonashworth/sandbar/blob/master/src/examples/se...
>
> Brenton
>
> On May 9, 11:24 pm, Shree Mulay  wrote:
>
>
>
>
>
>
>
> > For the life of me, I can't get sessions to work, immaterial of which
> > tutorial I try and get going???  Is there any tutorial out there that
> > explicitly explains everything for a newb like me? After several
> > round, I did successfully get form params to work! YEAH! But now, I'd
> > like to create a login so that the user can login and have state - but
> > I can't get SESSIONS TO WORK???
>
> > frustrated!,
>
> > shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
Dear Aaron, et al.,

I have been wrapping my sessions. Perhaps there's something I'm
missing???

(def app
(-> #'main-routes
(wrap-reload '(SocialNetworkCore.core))  ;;remove wrap-reload
from production code
(wrap-params)   ;; wraps params (inputs from
forms) - makes 'em available
;(wrap-flash);;
(wrap-session)  ;; wraps session
(wrap-request-logging)  ;; gives me request logging
(wrap-bounce-favicon)   ;; skirts favicon request logging
(wrap-exception-logging);; logs exceptions
(wrap-stacktrace))) ;; detailed stacktrace of errors

I'm using compojure's defroutes, as well. Let me know if you spot
anything that I may be missing, eh.

Thanks,

shree

On May 11, 7:32 pm, Aaron Cohen  wrote:
> The "magic" is in wrap-session, which adds the session middleware to your 
> stack.
>
> At that point, your ring handler will see that it's request map has a
> new key (:session) which is a map containing all your session
> variables.
>
> You can assoc anything into that map and it will be stored in the session.
>
> By default, this is all stored in-memory, it's possible to use a
> database instead if you specify options to wrap-session.
>
> See (doc wrap-session) 
> orhttp://clojuredocs.org/ring/ring.middleware.session/wrap-session
>
> From your gist:
>
> ; The next line is using destructuring to split out the session map
> into a local variable named "session".
> (defn handler [{session :session, uri :uri}]
> ; The next line is reading the ":n" value out of the session map.
>   (let [n (session :n 1)]

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
Dear James, et al.,

Here is what I'm trying to do:

---
;;Login form to login. To login successfully, let
;;password = secret
(defn login-view []
  (html
[:p
(form-to [:post "/login/" ]
  "User name: "
[:input {:name "name", :type "text"}]
[:br]
  "Password: "
[:input {:name "password", :type "password"}]
[:br]
  [:input {:type "submit" :value "Log in"}])]))


;;takes the input params from the login form and stores the
;;logged in user into a session. You get logged in when the
;;password value is = secret
(defn login-controller [params session]
  (do (println (params "password") (params "name")))
  (dosync
(if
  (= "secret" (params "password"))
; Username can include letters, numbers,
; spaces, underscores, and hyphens.
;(.matches (params :name) "[\\w\\s\\-]+"))
  (do
{:session (assoc session :name (params "name"))}
(redirect "/page/"))
  (redirect "/oops/"


;;In the repl, the following appears:
;;
;;Getting userid: nil
;;
(defn page [request session title body]
  (do
(println "Getting userid:" (:name session))
  )
)


;; I'm using compojure's defroutes (as you can see)
(defroutes main-routes
  (GET "/login/" [] (login-view))
  (POST "/login/" {params :params session :session} (login-controller
params session))
  (ANY "/page/" {request :request session :session} (page request
session "hello" "world"))
)

---

PROBLEM: I can't retrieve the session ":name" that I've been attempted
to store to. I don't know if it's getting stored into the session in
the first place. And finally, is it that I'm unable to retrieve it. I
know it's probably a very minor problem that needs to be fixed, but
I've been going up the wall on this one!

Thanks in advance,

shree

ps. My code may seem a bit contrived. It's been bundled together off
of a bunch of examples I've seen at the ring, compojure, sandbar
examples, and elsewhere...

On May 11, 8:01 pm, James Reeves  wrote:
> On 12 May 2011 00:04, Shree Mulay  wrote:
>
> > From this code, I can't figure out where you "instantiate" the session
> > var, store to it, and read back from it? I know there's not
> > instantiation in clojure(???). I hope your able to get what I'm having
> > trouble figuring out.
>
> The session is read from a :session key on the request map, and
> written using a :session key on the response map.
>
> For example, this route returns the value of the session key :x
>
>   (GET "/get" {session :session}
>     (str "x = " (:x session)))
>
> It's equivalent to the Sinatra route:
>
>   get "/get" do
>     "x = #{session[:x]}"
>   end
>
> The main difference is that the session has to be bound explicitly. We
> don't just get a magic session map hanging around in the background.
>
> Writing a session is a little trickier:
>
>   (GET "/set" [x :as {session :session}]
>     {:session (assoc session :x x)
>      :body (str "You set x = " x)})
>
> The equivalent Sinatra route is:
>
>   get "/set" do |x|
>     session[:x] = x
>     "You set x = #{x}"
>   end
>
> Ring sessions are a little unwieldy because you write by changing the
> response, rather than calling a side-effectful function. Sandbar takes
> a slightly different approach that looks more like the Sinatra code:
>
>   (GET "/get" []
>     (str "x = " (session-get :x)))
>
>   (GET "/set" [x]
>     (session-put! :x x)
>     (str "You set x = " x))
>
> Sandbar is a little cleaner for simple things, but Ring sessions have
> the advantage of working well with middleware. Idiomatic Ring tends to
> use a lot of middleware, far more than one might use in Rack (for
> instance).
>
> - James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Brenton
Shree,

I would suggest that you try to use only Ring first. Once you get how
it works, take a look at Sandbar and see if that may work better for
you in specific situations.

The session code in Sandbar is stable. It will not change much in the
next release. In fact it may become its own project by then.

Here is one small application that shows how to use both styles in one
app. I hope you find this helpful.

http://github.com/brentonashworth/sandbar/blob/master/src/examples/sessions/session_with_reload.clj

Brenton

On May 9, 11:24 pm, Shree Mulay  wrote:
> For the life of me, I can't get sessions to work, immaterial of which
> tutorial I try and get going???  Is there any tutorial out there that
> explicitly explains everything for a newb like me? After several
> round, I did successfully get form params to work! YEAH! But now, I'd
> like to create a login so that the user can login and have state - but
> I can't get SESSIONS TO WORK???
>
> frustrated!,
>
> shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread James Reeves
On 12 May 2011 00:04, Shree Mulay  wrote:
> From this code, I can't figure out where you "instantiate" the session
> var, store to it, and read back from it? I know there's not
> instantiation in clojure(???). I hope your able to get what I'm having
> trouble figuring out.

The session is read from a :session key on the request map, and
written using a :session key on the response map.

For example, this route returns the value of the session key :x

  (GET "/get" {session :session}
(str "x = " (:x session)))

It's equivalent to the Sinatra route:

  get "/get" do
"x = #{session[:x]}"
  end

The main difference is that the session has to be bound explicitly. We
don't just get a magic session map hanging around in the background.

Writing a session is a little trickier:

  (GET "/set" [x :as {session :session}]
{:session (assoc session :x x)
 :body (str "You set x = " x)})

The equivalent Sinatra route is:

  get "/set" do |x|
session[:x] = x
"You set x = #{x}"
  end

Ring sessions are a little unwieldy because you write by changing the
response, rather than calling a side-effectful function. Sandbar takes
a slightly different approach that looks more like the Sinatra code:

  (GET "/get" []
(str "x = " (session-get :x)))

  (GET "/set" [x]
(session-put! :x x)
(str "You set x = " x))

Sandbar is a little cleaner for simple things, but Ring sessions have
the advantage of working well with middleware. Idiomatic Ring tends to
use a lot of middleware, far more than one might use in Rack (for
instance).

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Aaron Cohen
The "magic" is in wrap-session, which adds the session middleware to your stack.

At that point, your ring handler will see that it's request map has a
new key (:session) which is a map containing all your session
variables.

You can assoc anything into that map and it will be stored in the session.

By default, this is all stored in-memory, it's possible to use a
database instead if you specify options to wrap-session.

See (doc wrap-session) or
http://clojuredocs.org/ring/ring.middleware.session/wrap-session

>From your gist:

; The next line is using destructuring to split out the session map
into a local variable named "session".
(defn handler [{session :session, uri :uri}]
; The next line is reading the ":n" value out of the session map.
  (let [n (session :n 1)]

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
Paul,

I'm using ring, hiccup, compojure, and clutch (for my database
connectivity via CouchDB). I'm avoiding Sandbar, cause I can't figure
too much of it out anyways) at this point, at least.

I think the only thing I'm using compojure for is [GET POST ANY
defroutes].

I'm building the "core component" for a social network project due for
my j2ee class tomorrow. I'm STUCK cause I can't figure out
sessions. :P

I've got clutch to work, html rendering, can pick up params from forms
using ring's wrap-params, etc...  I just can't get all the pieces to
come together - by having a users logged in state STORED into a
session!

I've been trying to remove all the complicated stuff and only focus on
the basic stuff. I don't need to have a real world set up for this
project, I just need my project to work.

The last part I've got to do is build "restful" services to allow my
component to coordinate with my other team member's components.
I'ven't tried to do this, yet, but have several pages up; Doesn't
appear to be something that should be a problem (hopefully).

BUT, I DO REQUIRE A SESSION - Unless there's another way???

Thanks guys,

shree

On May 11, 7:32 am, Paul Dorman  wrote:
> Hi Shree,
>
> while I wouldn't dare give you technical advice, I may have something
> to offer as a fellow Clojure noob. I did try Sandbar for my project,
> but found, as I delved deeper into the universe of Clojure web
> development, that I was altogether over-complicating everything. This,
> and the fact that Sandbar is undergoing something of a metamorphosis
> both the options of utilising the current stable release (which is
> likely to be relatively stagnant and unlikely to attract many more
> enhancements or documentation), and braving trunk (which is unstable,
> and unlikely to be adequately documented (for noobs, anyway) until at
> least some time after release).
>
> What I've discovered is that the whole deal is incredibly simple.
> Start with Ring. It's tiny, and all it does is speak html at one end
> (the end you don't need to care about unless you're a bona fide
> Clojurist), and Clojure at the other. Everything you need is available
> to you as Clojure maps, and all you have to do is write Clojure code
> to manipulate those maps. The bit between the request map and the
> response map are your Ring middlewares. Some of them might be part of
> Ring itself (such as the few bundled session middleware components
> which might be of use to you), middleware written by other people
> (which could do anything at all), and your own code.
>
> I have found that reducing the complexity of the tools you use greatly
> reduces the complexity of the task of gluing everything together and
> getting things working. Sandbar is a nice piece of work (props to
> Brendon), but as a noob, with simple requirements, I now understand
> that reaching for the most ornate magonoteis probably the last thing
> I should be doing, and that my fingers and a bit of determination are
> all I need to get some satisfaction and get my immediate problems
> solved.
>
> Not that I'm suggesting you do the same, but I'm using Ring,
> Compojure, Hiccup, and Clojureqlto serve up my application and manage
> sessions. Wiring those together with my application functions was very
> easily accomplished, with the vast majority of my time spent trying to
> get the presentation (HTML and CSS) right.
>
> Don't get allow yourself to get despondent - I really think you'll
> find solving your actual problems far simpler than you anticipated.
> It's just a matter of, well, 'getting it'.
>
> On May 11, 7:38 pm, Shree Mulay  wrote:
>
>
>
>
>
>
>
> > On May 10, 9:20 pm, Matthew Boston  wrote:
>
> > > Maybe try looking at the source of a project on github using logins/
> > > sessions.  One I'm currently using for reference is from 
> > > 4clojurehttps://github.com/dbyrne/4clojure
>
> > Hey Matthew,
>
> > I'm looking at the code. If I can't get headway on Ring, I might try
> > again with Sandbar.  The login at @https://github.com/dbyrne/4clojure/
> > blob/develop/src/foreclojure/login.clj might cover my requirements,
> > except that it uses mongodb as it's store...  hm
>
> > Worth looking at nonetheless.  :
>
> > Thanks,
>
> > shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
James,

>From this code, I can't figure out where you "instantiate" the session
var, store to it, and read back from it? I know there's not
instantiation in clojure(???). I hope your able to get what I'm having
trouble figuring out.

I'm not sure whether the magic happens here:
 (let [n (session :n 1)]
-or-
(assoc-in [:session :n] (inc n)))

In the code from: https://gist.github.com/608048.

Hope you get what I'm asking?

Thanks again,

shree

On May 11, 3:32 am, Shree Mulay  wrote:
> > There's an example of Ring sessions linked from the Ring wiki:
>
> >https://gist.github.com/608048
>
> > Does that help?
>
> > - James
>
> On May 10, 6:19 am, James Reeves  wrote:
>
>
>
>
>
>
>
>
>
> > On 10 May 2011 07:24, Shree Mulay  wrote:
>
> > > For the life of me, I can't get sessions to work, immaterial of which
> > > tutorial I try and get going???  Is there any tutorial out there that
> > > explicitly explains everything for a newb like me? After several
> > > round, I did successfully get form params to work! YEAH! But now, I'd
> > > like to create a login so that the user can login and have state - but
> > > I can't get SESSIONS TO WORK???
>
> > There's an example of Ring sessions linked from the Ring wiki:
>
> >https://gist.github.com/608048
>
> > Does that help?
>
> > - James
>
> @James, the problem with the example you've linked to, is it doesn't
> help me figure out how to link, for example, a userid picked up from a
> form into a session. Or how to set a session value to true/false,
> etc.
>
> I would appreciate a more comprehensive example, perhaps - a few more
> & simpler examples, etc. It's like compound example built into a
> single function. Also, would be nice to see session information passed
> around from one page to another (ie., routes???)... [is this even
> possible?]
>
> I don't want to use sandbar, either. Well, I couldn't figure out how
> to get its "wrap-stateful-session" to work properly.
>
> I'm trying to accomplish setting up a login form.
>
> At least I got rings params to work; I'd like to now have some of the
> information from submitted forms to get stored into session variables,
> eh...
>
> THANKS IN ADVANCE,
>
> shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Paul Dorman
Hi Shree,

while I wouldn't dare give you technical advice, I may have something
to offer as a fellow Clojure noob. I did try Sandbar for my project,
but found, as I delved deeper into the universe of Clojure web
development, that I was altogether over-complicating everything. This,
and the fact that Sandbar is undergoing something of a metamorphosis
both the options of utilising the current stable release (which is
likely to be relatively stagnant and unlikely to attract many more
enhancements or documentation), and braving trunk (which is unstable,
and unlikely to be adequately documented (for noobs, anyway) until at
least some time after release).

What I've discovered is that the whole deal is incredibly simple.
Start with Ring. It's tiny, and all it does is speak html at one end
(the end you don't need to care about unless you're a bona fide
Clojurist), and Clojure at the other. Everything you need is available
to you as Clojure maps, and all you have to do is write Clojure code
to manipulate those maps. The bit between the request map and the
response map are your Ring middlewares. Some of them might be part of
Ring itself (such as the few bundled session middleware components
which might be of use to you), middleware written by other people
(which could do anything at all), and your own code.

I have found that reducing the complexity of the tools you use greatly
reduces the complexity of the task of gluing everything together and
getting things working. Sandbar is a nice piece of work (props to
Brendon), but as a noob, with simple requirements, I now understand
that reaching for the most ornate magonote is probably the last thing
I should be doing, and that my fingers and a bit of determination are
all I need to get some satisfaction and get my immediate problems
solved.

Not that I'm suggesting you do the same, but I'm using Ring,
Compojure, Hiccup, and Clojureql to serve up my application and manage
sessions. Wiring those together with my application functions was very
easily accomplished, with the vast majority of my time spent trying to
get the presentation (HTML and CSS) right.

Don't get allow yourself to get despondent - I really think you'll
find solving your actual problems far simpler than you anticipated.
It's just a matter of, well, 'getting it'.

On May 11, 7:38 pm, Shree Mulay  wrote:
> On May 10, 9:20 pm, Matthew Boston  wrote:
>
> > Maybe try looking at the source of a project on github using logins/
> > sessions.  One I'm currently using for reference is from 
> > 4clojurehttps://github.com/dbyrne/4clojure
>
> Hey Matthew,
>
> I'm looking at the code. If I can't get headway on Ring, I might try
> again with Sandbar.  The login at @https://github.com/dbyrne/4clojure/
> blob/develop/src/foreclojure/login.clj might cover my requirements,
> except that it uses mongodb as it's store...  hm
>
> Worth looking at nonetheless.  :
>
> Thanks,
>
> shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay


On May 10, 9:20 pm, Matthew Boston  wrote:
> Maybe try looking at the source of a project on github using logins/
> sessions.  One I'm currently using for reference is from 
> 4clojurehttps://github.com/dbyrne/4clojure
>

Hey Matthew,

I'm looking at the code. If I can't get headway on Ring, I might try
again with Sandbar.  The login at @https://github.com/dbyrne/4clojure/
blob/develop/src/foreclojure/login.clj might cover my requirements,
except that it uses mongodb as it's store...  hm

Worth looking at nonetheless.  :

Thanks,

shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-11 Thread Shree Mulay
> There's an example of Ring sessions linked from the Ring wiki:
>
> https://gist.github.com/608048
>
> Does that help?
>
> - James


On May 10, 6:19 am, James Reeves  wrote:
> On 10 May 2011 07:24, Shree Mulay  wrote:
>
> > For the life of me, I can't get sessions to work, immaterial of which
> > tutorial I try and get going???  Is there any tutorial out there that
> > explicitly explains everything for a newb like me? After several
> > round, I did successfully get form params to work! YEAH! But now, I'd
> > like to create a login so that the user can login and have state - but
> > I can't get SESSIONS TO WORK???
>
> There's an example of Ring sessions linked from the Ring wiki:
>
> https://gist.github.com/608048
>
> Does that help?
>
> - James

@James, the problem with the example you've linked to, is it doesn't
help me figure out how to link, for example, a userid picked up from a
form into a session. Or how to set a session value to true/false,
etc.

I would appreciate a more comprehensive example, perhaps - a few more
& simpler examples, etc. It's like compound example built into a
single function. Also, would be nice to see session information passed
around from one page to another (ie., routes???)... [is this even
possible?]

I don't want to use sandbar, either. Well, I couldn't figure out how
to get its "wrap-stateful-session" to work properly.

I'm trying to accomplish setting up a login form.

At least I got rings params to work; I'd like to now have some of the
information from submitted forms to get stored into session variables,
eh...

THANKS IN ADVANCE,

shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-10 Thread Matthew Boston
Maybe try looking at the source of a project on github using logins/
sessions.  One I'm currently using for reference is from 4clojure
https://github.com/dbyrne/4clojure

On May 10, 2:24 am, Shree Mulay  wrote:
> For the life of me, I can't get sessions to work, immaterial of which
> tutorial I try and get going???  Is there any tutorial out there that
> explicitly explains everything for a newb like me? After several
> round, I did successfully get form params to work! YEAH! But now, I'd
> like to create a login so that the user can login and have state - but
> I can't get SESSIONS TO WORK???
>
> frustrated!,
>
> shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-10 Thread James Reeves
On 10 May 2011 07:24, Shree Mulay  wrote:
> For the life of me, I can't get sessions to work, immaterial of which
> tutorial I try and get going???  Is there any tutorial out there that
> explicitly explains everything for a newb like me? After several
> round, I did successfully get form params to work! YEAH! But now, I'd
> like to create a login so that the user can login and have state - but
> I can't get SESSIONS TO WORK???

There's an example of Ring sessions linked from the Ring wiki:

https://gist.github.com/608048

Does that help?

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


sessions in Ring, Sandbar, Compojure, etc...

2011-05-09 Thread Shree Mulay
For the life of me, I can't get sessions to work, immaterial of which
tutorial I try and get going???  Is there any tutorial out there that
explicitly explains everything for a newb like me? After several
round, I did successfully get form params to work! YEAH! But now, I'd
like to create a login so that the user can login and have state - but
I can't get SESSIONS TO WORK???

frustrated!,

shree

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en