store

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 10 Imported by: 4

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 {
	// DBOptions represents options for a database.
	DBOptions Options
	MaxLength 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

func New(db *bolt.DB, config Config, keyPairs ...[]byte) (*Store, error)

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"
Output:

func (*Store) Get

func (s *Store) Get(ctx echo.Context, name string) (*sessions.Session, error)

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"
Output:

func (*Store) MaxLength added in v1.1.1

func (s *Store) MaxLength(l int)

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

func (s *Store) New(ctx echo.Context, name string) (*sessions.Session, error)

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"
Output:

func (*Store) Reload added in v1.0.1

func (s *Store) Reload(ctx echo.Context, session *sessions.Session) error

func (*Store) Remove added in v1.1.0

func (s *Store) Remove(sessionID string) error

Remove removes the key-value from the database.

func (*Store) Save

func (s *Store) Save(ctx echo.Context, session *sessions.Session) error

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)
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL