guillep2k cedb285e25 Update github.com/lafriks/xormstore and tidy up mod.go (#8020) | 5 년 전 | |
---|---|---|
.. | ||
AUTHORS | 5 년 전 | |
LICENSE | 5 년 전 | |
README.md | 5 년 전 | |
cookie.go | 5 년 전 | |
cookie_go111.go | 5 년 전 | |
doc.go | 5 년 전 | |
go.mod | 5 년 전 | |
go.sum | 5 년 전 | |
lex.go | 7 년 전 | |
options.go | 5 년 전 | |
options_go111.go | 5 년 전 | |
sessions.go | 5 년 전 | |
store.go | 5 년 전 |
gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.
The key features are:
Let’s start with an example that shows the sessions API in a nutshell:
import (
"net/http"
"github.com/gorilla/sessions"
)
// Note: Don't store your key in your source code. Pass it via an
// environmental variable, or flag (or both), and don't accidentally commit it
// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
func MyHandler(w http.ResponseWriter, r *http.Request) {
// Get a session. We're ignoring the error resulted from decoding an
// existing session: Get() always returns a session, even if empty.
session, _ := store.Get(r, "session-name")
// Set some session values.
session.Values["foo"] = "bar"
session.Values[42] = 43
// Save it before we write to the response/return from the handler.
session.Save(r, w)
}
First we initialize a session store calling NewCookieStore()
and passing a
secret key used to authenticate the session. Inside the handler, we call
store.Get()
to retrieve an existing session or create a new one. Then we set
some session values in session.Values, which is a map[interface{}]interface{}
.
And finally we call session.Save()
to save the session in the response.
More examples are available on the Gorilla website.
Other implementations of the sessions.Store
interface:
BSD licensed. See the LICENSE file for details.