Documentation
¶
Overview ¶
Package store provides a session store using Bolt.
Index ¶
- type Config
- type Options
- type Store
- func (s *Store) Get(ctx echo.Context, name string) (*sessions.Session, error)
- func (s *Store) MaxLength(l int)
- func (s *Store) New(ctx echo.Context, name string) (*sessions.Session, error)
- func (s *Store) Reload(ctx echo.Context, session *sessions.Session) error
- func (s *Store) Remove(sessionID string) error
- func (s *Store) Save(ctx echo.Context, session *sessions.Session) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// DBOptions represents options for a database.
DBOptions Options
MaxLength int
EmptyDataAge int
}
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)
}
w := httptest.NewRecorder()
ctx := echo.NewContext(mock.NewRequest(r), mock.NewResponse(w), defaults.Default)
// Get a session.
session, err := str.Get(ctx, "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)
}
w := httptest.NewRecorder()
ctx := echo.NewContext(mock.NewRequest(r), mock.NewResponse(w), defaults.Default)
// Get a session.
session, err := str.Get(ctx, "session-key")
if err != nil {
panic(err)
}
// Add a value on the session.
session.Values["foo"] = "bar"
func (*Store) MaxLength ¶ added in v1.1.1
MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new FilesystemStore is 4096.
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)
}
w := httptest.NewRecorder()
ctx := echo.NewContext(mock.NewRequest(r), mock.NewResponse(w), defaults.Default)
// Create a session.
session, err := str.New(ctx, "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)
}
ctx := echo.NewContext(mock.NewRequest(r), mock.NewResponse(w), defaults.Default)
// Create a session.
session, err := str.New(ctx, "session-key")
if err != nil {
panic(err)
}
// Add a value on the session.
session.Values["foo"] = "bar"
// Save the session.
if err := sessions.Save(ctx); err != nil {
panic(err)
}
// You can delete the session by setting the session options's MaxAge
// to a minus value.
ctx.SessionOptions().MaxAge = -1
if err := sessions.Save(ctx); err != nil {
panic(err)
}