• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H28-Sep-2018-95

.travis.ymlH A D28-Sep-2018474 2925

AUTHORSH A D28-Sep-20181.6 KiB4442

LICENSEH A D28-Sep-20181.5 KiB2824

README.mdH A D28-Sep-20184.9 KiB9677

cookie.goH A D28-Sep-2018409 2013

cookie_go111.goH A D28-Sep-2018438 2114

cookie_go111_test.goH A D28-Sep-2018614 3023

cookie_test.goH A D28-Sep-20181.8 KiB5854

doc.goH A D28-Sep-20187.4 KiB2031

go.modH A D28-Sep-2018129 75

lex.goH A D28-Sep-20181.5 KiB10394

options.goH A D28-Sep-2018478 198

options_go111.goH A D28-Sep-2018561 2310

sessions.goH A D28-Sep-20185.7 KiB219147

sessions_test.goH A D28-Sep-20184.8 KiB186135

store.goH A D28-Sep-20188.4 KiB293180

store_test.goH A D28-Sep-20183.1 KiB130105

README.md

1# sessions
2
3[![GoDoc](https://godoc.org/github.com/gorilla/sessions?status.svg)](https://godoc.org/github.com/gorilla/sessions) [![Build Status](https://travis-ci.org/gorilla/sessions.svg?branch=master)](https://travis-ci.org/gorilla/sessions)
4[![Sourcegraph](https://sourcegraph.com/github.com/gorilla/sessions/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/sessions?badge)
5
6gorilla/sessions provides cookie and filesystem sessions and infrastructure for
7custom session backends.
8
9The key features are:
10
11- Simple API: use it as an easy way to set signed (and optionally
12  encrypted) cookies.
13- Built-in backends to store sessions in cookies or the filesystem.
14- Flash messages: session values that last until read.
15- Convenient way to switch session persistency (aka "remember me") and set
16  other attributes.
17- Mechanism to rotate authentication and encryption keys.
18- Multiple sessions per request, even using different backends.
19- Interfaces and infrastructure for custom session backends: sessions from
20  different stores can be retrieved and batch-saved using a common API.
21
22Let's start with an example that shows the sessions API in a nutshell:
23
24```go
25	import (
26		"net/http"
27		"github.com/gorilla/sessions"
28	)
29
30	// Note: Don't store your key in your source code. Pass it via an
31	// environmental variable, or flag (or both), and don't accidentally commit it
32	// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
33	// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
34	var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY"))
35
36	func MyHandler(w http.ResponseWriter, r *http.Request) {
37		// Get a session. We're ignoring the error resulted from decoding an
38		// existing session: Get() always returns a session, even if empty.
39		session, _ := store.Get(r, "session-name")
40		// Set some session values.
41		session.Values["foo"] = "bar"
42		session.Values[42] = 43
43		// Save it before we write to the response/return from the handler.
44		session.Save(r, w)
45	}
46```
47
48First we initialize a session store calling `NewCookieStore()` and passing a
49secret key used to authenticate the session. Inside the handler, we call
50`store.Get()` to retrieve an existing session or create a new one. Then we set
51some session values in session.Values, which is a `map[interface{}]interface{}`.
52And finally we call `session.Save()` to save the session in the response.
53
54Important Note: If you aren't using gorilla/mux, you need to wrap your handlers
55with
56[`context.ClearHandler`](http://www.gorillatoolkit.org/pkg/context#ClearHandler)
57or else you will leak memory! An easy way to do this is to wrap the top-level
58mux when calling http.ListenAndServe:
59
60```go
61	http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))
62```
63
64The ClearHandler function is provided by the gorilla/context package.
65
66More examples are available [on the Gorilla
67website](http://www.gorillatoolkit.org/pkg/sessions).
68
69## Store Implementations
70
71Other implementations of the `sessions.Store` interface:
72
73- [github.com/starJammer/gorilla-sessions-arangodb](https://github.com/starJammer/gorilla-sessions-arangodb) - ArangoDB
74- [github.com/yosssi/boltstore](https://github.com/yosssi/boltstore) - Bolt
75- [github.com/srinathgs/couchbasestore](https://github.com/srinathgs/couchbasestore) - Couchbase
76- [github.com/denizeren/dynamostore](https://github.com/denizeren/dynamostore) - Dynamodb on AWS
77- [github.com/savaki/dynastore](https://github.com/savaki/dynastore) - DynamoDB on AWS (Official AWS library)
78- [github.com/bradleypeabody/gorilla-sessions-memcache](https://github.com/bradleypeabody/gorilla-sessions-memcache) - Memcache
79- [github.com/dsoprea/go-appengine-sessioncascade](https://github.com/dsoprea/go-appengine-sessioncascade) - Memcache/Datastore/Context in AppEngine
80- [github.com/kidstuff/mongostore](https://github.com/kidstuff/mongostore) - MongoDB
81- [github.com/srinathgs/mysqlstore](https://github.com/srinathgs/mysqlstore) - MySQL
82- [github.com/EnumApps/clustersqlstore](https://github.com/EnumApps/clustersqlstore) - MySQL Cluster
83- [github.com/antonlindstrom/pgstore](https://github.com/antonlindstrom/pgstore) - PostgreSQL
84- [github.com/boj/redistore](https://github.com/boj/redistore) - Redis
85- [github.com/boj/rethinkstore](https://github.com/boj/rethinkstore) - RethinkDB
86- [github.com/boj/riakstore](https://github.com/boj/riakstore) - Riak
87- [github.com/michaeljs1990/sqlitestore](https://github.com/michaeljs1990/sqlitestore) - SQLite
88- [github.com/wader/gormstore](https://github.com/wader/gormstore) - GORM (MySQL, PostgreSQL, SQLite)
89- [github.com/gernest/qlstore](https://github.com/gernest/qlstore) - ql
90- [github.com/quasoft/memstore](https://github.com/quasoft/memstore) - In-memory implementation for use in unit tests
91- [github.com/lafriks/xormstore](https://github.com/lafriks/xormstore) - XORM (MySQL, PostgreSQL, SQLite, Microsoft SQL Server, TiDB)
92
93## License
94
95BSD licensed. See the LICENSE file for details.
96