gorilla sessions tutorial

1,307 views
Skip to first unread message

Sankar

unread,
Jun 21, 2015, 10:54:16 PM6/21/15
to golan...@googlegroups.com
Hi

Can any of you point me to a tutorial / example / code where it is explained how to implement sessions using gorilla ?

I want to write a quiz webapp, where I have to ask each user 10 random questions, one after another. In the first page I will get the username/email address, ask 10 questions and then display his marks by the end of questions.

Since there may be multiple users accessing the site in parallel, I need a nice way to maintain the session on the server for each unique user/browser-tab.

As such there is no authorization or even authentication. I just need a way to get the email address in teh first page, maintain a question-counter for each question asked, aggregate marks for each user/request. It may also happen that two different users from two different places may use the same dummy email address.

Any pointers ? This is just a dummy application to understand how HTTP sessions work and in using gorilla. It is not going to be in production, except for a few demos to some friends. So I am not overly concerned with passwords, hashes, salt etc.

I already looked at http://www.gorillatoolkit.org/pkg/sessions but that is not very elaborate.

Thanks.

Sankar

Matt Silverlock

unread,
Jun 22, 2015, 8:26:33 AM6/22/15
to golan...@googlegroups.com
The example at the top of http://www.gorillatoolkit.org/pkg/sessions should cover the basics.
  • The default sessions.CookieStore provides a unique-per-user cookie (browser tab doesn't matter - cookies are per domain)
  • If you want to store anything in the cookie there's a 4KB max limit per domain.
  • You'll likely need to figure out a way to store the marks for each user. Storing them in a cookie might be too much.
  • A database would be simple. sqlite3 via the sqlx (https://github.com/jmoiron/sqlx) package or BoltDB (https://github.com/boltdb/bolt) are both simple file-based stores that'd likely be fine for starting out with
  • Authentication/authorisation is definitely not necessary for something simple
Here's a slightly modified example based on the gorilla/sessions example: http://play.golang.org/p/3PpNMXgVCi — note that it's designed to demonstrate where to get started, not resolve all of the loose ends!
Reply all
Reply to author
Forward
0 new messages