Documentation ¶
Overview ¶
Package gsb session support for Gorilla Web Toolkit
Index ¶
- Constants
- Variables
- type BigCacher
- type BigcacheStore
- func NewBigCacherStore(client BigCacher, keyPrefix string, keyPairs ...[]byte) *BigcacheStore
- func NewBigCacherStoreWithValueStorer(client BigCacher, valueStorer ValueStorer, keyPrefix string, ...) *BigcacheStore
- func NewBigcacheStore(client *bigcache.BigCache, keyPrefix string, keyPairs ...[]byte) *BigcacheStore
- func NewBigcacheStoreWithValueStorer(client *bigcache.BigCache, valueStorer ValueStorer, keyPrefix string, ...) *BigcacheStore
- func (s *BigcacheStore) Get(r *http.Request, name string) (*sessions.Session, error)
- func (s *BigcacheStore) MaxLength(l int)
- func (s *BigcacheStore) New(r *http.Request, name string) (*sessions.Session, error)
- func (s *BigcacheStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
- type CookieStorer
- type DumbMemoryStore
- func (s *DumbMemoryStore) Get(r *http.Request, name string) (*sessions.Session, error)
- func (s *DumbMemoryStore) MaxLength(l int)
- func (s *DumbMemoryStore) New(r *http.Request, name string) (*sessions.Session, error)
- func (s *DumbMemoryStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
- type GoBigcacher
- type HeaderStorer
- type StoreMethod
- type ValueStorer
Examples ¶
Constants ¶
const ( StoreMethodSecureCookie = StoreMethod("securecookie") // security StoreMethodGob = StoreMethod("gob") // speed StoreMethodJson = StoreMethod("json") // simplicity; warning: only string keys allowed and rest of data must be JSON.Marshal compatible )
take your pick on how to store the values in bigcache
Variables ¶
var ( // ErrHeaderFieldNameEmpty is returned, if the HeaderFieldName, which should be used to store session information, is empty. ErrHeaderFieldNameEmpty = errors.New("header fieldname empty") // ErrValueNotFound is returned, if no value was found for a given sessionName. ErrValueNotFound = errors.New("value not found") )
Functions ¶
This section is empty.
Types ¶
type BigCacher ¶
type BigCacher interface { Get(key string) (string, error) Set(key, val string, exp uint32, ocas uint64) (cas uint64, err error) }
BigCacher is the interface gsb uses to interact with the bigcache client
type BigcacheStore ¶
type BigcacheStore struct { Codecs []securecookie.Codec Options *sessions.Options // default configuration Client BigCacher KeyPrefix string Logging int // set to > 0 to enable logging (using log.Printf) StoreMethod StoreMethod ValueStorer ValueStorer }
BigcacheStore stores sessions in bigcache
func NewBigCacherStore ¶
func NewBigCacherStore(client BigCacher, keyPrefix string, keyPairs ...[]byte) *BigcacheStore
NewBigCacherStore returns a new BigcacheStore. You need to provide the bigcache client that implements the BigCacher interface and an optional prefix for the keys we store
func NewBigCacherStoreWithValueStorer ¶
func NewBigCacherStoreWithValueStorer(client BigCacher, valueStorer ValueStorer, keyPrefix string, keyPairs ...[]byte) *BigcacheStore
NewBigCacherStoreWithValueStorer returns a new BigcacheStore backed by a ValueStorer. You need to provide the bigcache client that implements the BigCacher interface and an optional prefix for the keys we store. A ValueStorer is used to store an encrypted sessionID. The encrypted sessionID is used to access bigcache and get the session values.
func NewBigcacheStore ¶
func NewBigcacheStore(client *bigcache.BigCache, keyPrefix string, keyPairs ...[]byte) *BigcacheStore
NewBigcacheStore returns a new BigcacheStore for the gobigcache client (github.com/allegro/bigcache). You also need to provider an optional prefix for the keys we store.
Example ¶
package main import ( "fmt" "net/http" "time" "github.com/allegro/bigcache/v2" gsb "github.com/jtorz/gorilla-sessions-bigcache" ) func main() { bigcacheClient, err := bigcache.NewBigCache(bigcache.DefaultConfig(10 * time.Minute)) if err != nil { panic(err) } store := gsb.NewBigcacheStore(bigcacheClient, "session_prefix_", []byte("secret")) //store := gsb.NewBigCacherStoreWithValueStorer(gsb.NewGoBigcacher(bigcacheClient), &gsb.HeaderStorer{HeaderFieldName: "X-CUSTOM-HEADER"}, "session_prefix_", []byte("secret-key-goes-here")) runServer(store) } func runServer(store *gsb.BigcacheStore) { http.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) { session, err := store.Get(r, "session-name") if err != nil { fmt.Fprintf(w, "Error: %v", err) return } fmt.Fprintf(w, "Got: %v", session) }) http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { session, err := store.Get(r, "session-name") if err != nil { fmt.Fprintf(w, "Error: %v", err) return } session.Values["foo"] = "bar" session.Values[42] = 43 session.Save(r, w) fmt.Fprint(w, "ok") }) http.ListenAndServe(":8080", nil) }
Output:
func NewBigcacheStoreWithValueStorer ¶
func NewBigcacheStoreWithValueStorer(client *bigcache.BigCache, valueStorer ValueStorer, keyPrefix string, keyPairs ...[]byte) *BigcacheStore
NewBigcacheStoreWithValueStorer returns a new BigcacheStore backed by a ValueStorer. You need to provide the gobigcache client (github.com/allegro/bigcache) and an optional prefix for the keys we store. A ValueStorer is used to store an encrypted sessionID. The encrypted sessionID is used to access bigcache and get the session values.
func (*BigcacheStore) Get ¶
Get returns a session for the given name after adding it to the registry.
See CookieStore.Get().
func (*BigcacheStore) MaxLength ¶
func (s *BigcacheStore) 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 BigcacheStore is 4096.
func (*BigcacheStore) New ¶
New returns a session for the given name without adding it to the registry.
See CookieStore.New().
func (*BigcacheStore) Save ¶
func (s *BigcacheStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
Save adds a single session to the response.
type CookieStorer ¶
type CookieStorer struct{}
CookieStorer is a ValueStorer, which stores values inside an http.Cookie
func (*CookieStorer) GetValueForSessionName ¶
GetValueForSessionName gets a value string from an http.Cookie, which should be present in the http.Request.
func (*CookieStorer) SetValueForSessionName ¶
func (s *CookieStorer) SetValueForSessionName(w http.ResponseWriter, name, value string, options *sessions.Options) error
SetValueForSessionName sets a value string by creating a new http.Cookie and setting a `Set-Cookie` header
type DumbMemoryStore ¶
type DumbMemoryStore struct { Codecs []securecookie.Codec Options *sessions.Options // default configuration Data map[string]string // session data goes here ValueStorer ValueStorer }
DumbMemoryStore stores sessions in bigcache
func NewDumbMemorySessionStore ¶
func NewDumbMemorySessionStore() *DumbMemoryStore
NewDumbMemorySessionStore Sessions implemented with a dumb in-memory map and no expiration. Good for local development so you don't have to run bigcached on your laptop just to fire up your app and hack away.
func NewDumbMemorySessionStoreWithValueStorer ¶
func NewDumbMemorySessionStoreWithValueStorer(valueStorer ValueStorer) *DumbMemoryStore
NewDumbMemorySessionStoreWithValueStorer return a new dumb in-memory map and no expiration backed by a ValueStorer. Good for local development so you don't have to run bigcached on your laptop just to fire up your app and hack away. A ValueStorer is used to store an encrypted sessionID. The encrypted sessionID is used to access the dumb in-memory map and get the session values.
func (*DumbMemoryStore) Get ¶
Get returns a session for the given name after adding it to the registry.
See CookieStore.Get().
func (*DumbMemoryStore) MaxLength ¶
func (s *DumbMemoryStore) 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 DumbMemoryStore is 4096.
func (*DumbMemoryStore) New ¶
New returns a session for the given name without adding it to the registry.
See CookieStore.New().
func (*DumbMemoryStore) Save ¶
func (s *DumbMemoryStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
Save adds a single session to the response.
type GoBigcacher ¶
type GoBigcacher struct {
// contains filtered or unexported fields
}
GoBigcacher is a wrapper to the gobigcache client that implements the BigCacher interface
func NewGoBigcacher ¶
func NewGoBigcacher(c *bigcache.BigCache) *GoBigcacher
NewGoBigcacher returns a wrapped gobigcache client that implements the BigCacher interface
type HeaderStorer ¶
type HeaderStorer struct {
HeaderFieldName string
}
HeaderStorer is a ValueStorer, which stores values inside an http Header. The key of the header contains can be configured using the `HeaderFieldName` variable. The header value is a Base64 encoded JSON map, whereas the keys of the map are the sessionName.
func (*HeaderStorer) GetValueForSessionName ¶
GetValueForSessionName gets a value string from an http.Header.
func (*HeaderStorer) SetValueForSessionName ¶
func (s *HeaderStorer) SetValueForSessionName(w http.ResponseWriter, name, value string, options *sessions.Options) error
SetValueForSessionName sets a value string by creating a new http.Header using the header key given by the headerStorer.HeaderKey function.
type StoreMethod ¶
type StoreMethod string
type ValueStorer ¶
type ValueStorer interface { // GetValueForSessionName gets a value string using it's underlying ValueStorer implementation. GetValueForSessionName(r *http.Request, name string) (string, error) // SetValueForSessionName sets a value string using it's underlying ValueStorer implementation. SetValueForSessionName(w http.ResponseWriter, name, value string, options *sessions.Options) error }
ValueStorer stores a value for a given name inside a http.Request. The value is typically the encrypted sessionID, which can then be fetched by a Gorialla sessions.Store implementation.