store

package
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2014 License: MIT Imports: 10 Imported by: 0

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

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

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

func (*Store) Get

func (s *Store) Get(r *http.Request, 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)
}

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

func (*Store) New

func (s *Store) New(r *http.Request, 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)
}

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

func (*Store) Save

func (s *Store) Save(r *http.Request, w http.ResponseWriter, 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)
}

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

Jump to

Keyboard shortcuts

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