Documentation
¶
Overview ¶
Package store provides a session store using Bolt.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// SessionOptions represents options for a session.
SessionOptions sessions.Options
// DBOptions represents options for a database.
DBOptions Options
}
Config represents a config for a session store.
type Options ¶
type Options struct {
// BucketName represents the name of the bucket which contains sessions.
BucketName []byte
}
Options represents options for a database.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store represents a session store.
func New ¶
New creates and returns a session store.
Example ¶
// db should be opened beforehand and passed by the other function.
var db *bolt.DB
// r(*http.Request) should be passed by the other function.
var r *http.Request
// Create a store.
str, err := New(db, Config{}, []byte("secret-key"))
if err != nil {
panic(err)
}
// Get a session.
session, err := str.Get(r, "session-key")
if err != nil {
panic(err)
}
// Add a value on the session.
session.Values["foo"] = "bar"
func (*Store) Get ¶
Get returns a session for the given name after adding it to the registry.
See gorilla/sessions FilesystemStore.Get().
Example ¶
// db(*bolt.DB) should be opened beforehand and passed by the other function.
var db *bolt.DB
// r(*http.Request) should be passed by the other function.
var r *http.Request
// Create a store.
str, err := New(db, Config{}, []byte("secret-key"))
if err != nil {
panic(err)
}
// Get a session.
session, err := str.Get(r, "session-key")
if err != nil {
panic(err)
}
// Add a value on the session.
session.Values["foo"] = "bar"
func (*Store) New ¶
New returns a session for the given name without adding it to the registry.
See gorilla/sessions FilesystemStore.New().
Example ¶
// db(*bolt.DB) should be opened beforehand and passed by the other function.
var db *bolt.DB
// r(*http.Request) should be passed by the other function.
var r *http.Request
// Create a store.
str, err := New(db, Config{}, []byte("secret-key"))
if err != nil {
panic(err)
}
// Create a session.
session, err := str.New(r, "session-key")
if err != nil {
panic(err)
}
// Add a value on the session.
session.Values["foo"] = "bar"
func (*Store) Save ¶
Save adds a single session to the response.
Example ¶
// db(*bolt.DB) should be opened beforehand and passed by the other function.
var db *bolt.DB
// w(http.ResponseWriter) should be passed by the other function.
var w http.ResponseWriter
// r(*http.Request) should be passed by the other function.
var r *http.Request
// Create a store.
str, err := New(db, Config{}, []byte("secret-key"))
if err != nil {
panic(err)
}
// Create a session.
session, err := str.New(r, "session-key")
if err != nil {
panic(err)
}
// Add a value on the session.
session.Values["foo"] = "bar"
// Save the session.
if err := sessions.Save(r, w); err != nil {
panic(err)
}
// You can delete the session by setting the session options's MaxAge
// to a minus value.
session.Options.MaxAge = -1
if err := sessions.Save(r, w); err != nil {
panic(err)
}
Click to show internal directories.
Click to hide internal directories.