storage

package
v0.0.0-...-0cbb5ad Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddConfigBuilder

func AddConfigBuilder(t Type, f ConfigBuilder)

Types

type Config

type Config interface {
	Open() (*Storage, error)
	SetSecureCookie(val bool)
}

Config is a config interface that every Conn SessionConfig would implement it

func BuildStorageConfig

func BuildStorageConfig(t Type) (Config, error)

type ConfigBuilder

type ConfigBuilder func() Config

type Conn

type Conn interface {
	// Load reads the session from the database.
	// returns true if there is a session data in DB
	Load(session *sessions.Session) (bool, error)
	// Save stores the session in the database.
	Save(session *sessions.Session) error
	// Delete removes keys from the database if MaxAge<0
	Delete(session *sessions.Session) error
	// Close closes the database.
	Close() error
}

Conn is the interface for underlying persistent database

type SessionConfig

type SessionConfig struct {
	// KeyPairs are used to generate securecookie.Codec,
	// Should not change them after application is started,
	// otherwise previously issued cookies will not be able to be decoded.
	// Can be created using securecookie.GenerateRandomKey()
	KeyPairs []string `json:"keyPairs"`
	// Session Max-Age attribute present and given in seconds.
	MaxAge int `json:"maxAge"`
	// contains filtered or unexported fields
}

func (*SessionConfig) SetSecureCookie

func (sc *SessionConfig) SetSecureCookie(val bool)

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage is a custom session store which provides an abstraction of common session store operations for multiple Key/Value databases.

func New

func New(conn Conn, config SessionConfig) *Storage

func (*Storage) Close

func (s *Storage) Close() error

func (*Storage) Get

func (s *Storage) Get(r *http.Request, name string) (*sessions.Session, error)

Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded or be expired.

func (*Storage) MaxAge

func (s *Storage) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting options.MaxAge = -1 for that session.

func (*Storage) New

func (s *Storage) New(r *http.Request, name string) (session *sessions.Session, err error)

New returns a session for the given name without adding it to the registry.

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call. Get() calls New() internally if there's no data in cache.

func (*Storage) Save

func (s *Storage) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save saves a single session to the underlying database and save the encoded session id to cookie of the response.

If the options.MaxAge of the session is <= 0 then the session will be deleted from the store path. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.

type Type

type Type string
const (
	BOLT   Type = "bolt"
	REDIS  Type = "redis"
	MEMORY Type = "memory"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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