session

package module
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2020 License: MIT Imports: 9 Imported by: 3

README

session

Build Status Go Report Card GoDev GitHub release

Provide session storage to fasthttp.

This package follow the fasthttp philosophy, trying to avoid extra memory allocations in hot paths.

See examples to see how to use it.

Providers

  • memory
  • memcache
  • mysql
  • postgres
  • redis
  • sqlite3

Features

  • Focus on the design of the code architecture and expansion.
  • Provide full session storage.
  • Convenient switching of session storage.
  • Customizable data serialization.

Bugs

If you find a bug, please open new issue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(providerName string, provider Provider) error

Register register session provider

Types

type Config

type Config struct {

	// cookie name
	CookieName string

	// cookie domain
	Domain string

	// If you want to delete the cookie when the browser closes, set it to -1.
	//
	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	Expires time.Duration

	// gc life time to execute it
	GCLifetime time.Duration

	// set whether to pass this bar cookie only through HTTPS
	Secure bool

	// sessionID is in url query
	SessionIDInURLQuery bool

	// sessionName in url query
	SessionNameInURLQuery string

	// sessionID is in http header
	SessionIDInHTTPHeader bool

	// sessionName in http header
	SessionNameInHTTPHeader string

	// SessionIDGeneratorFunc should returns a random session id.
	SessionIDGeneratorFunc func() []byte

	// IsSecureFunc should return whether the communication channel is secure
	// in order to set the secure flag to true according to Secure flag.
	IsSecureFunc func(*fasthttp.RequestCtx) bool
	// contains filtered or unexported fields
}

Config config struct

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig return new default configuration

type Cookie struct{}

Cookie cookie struct

func NewCookie

func NewCookie() *Cookie

NewCookie return new cookie instance

func (*Cookie) Delete

func (c *Cookie) Delete(ctx *fasthttp.RequestCtx, name string)

Delete delete cookie by cookie name

func (*Cookie) Get

func (c *Cookie) Get(ctx *fasthttp.RequestCtx, name string) []byte

Get get cookie by name

func (*Cookie) Set

func (c *Cookie) Set(ctx *fasthttp.RequestCtx, name string, value []byte, domain string, expires time.Duration, secure bool)

Set response set cookie

type Dao

type Dao struct {
	dao.Dao
}

Dao database connection

type Dict

type Dict struct {
	dictpool.Dict
}

Dict memory store

type Encrypt

type Encrypt struct{}

Encrypt encrypt struct

func NewEncrypt

func NewEncrypt() *Encrypt

NewEncrypt return new encrypt instance

func (*Encrypt) Base64Decode

func (e *Encrypt) Base64Decode(dst *Dict, src []byte) error

Base64Decode base64 decode

func (*Encrypt) Base64Encode

func (e *Encrypt) Base64Encode(src Dict) ([]byte, error)

Base64Encode base64 encode

func (*Encrypt) MSGPDecode

func (e *Encrypt) MSGPDecode(dst *Dict, src []byte) error

MSGPDecode MessagePack decode

func (*Encrypt) MSGPEncode

func (e *Encrypt) MSGPEncode(src Dict) ([]byte, error)

MSGPEncode MessagePack encode

type Provider

type Provider interface {
	Init(expiration time.Duration, cfg ProviderConfig) error
	Get(id []byte) (Storer, error)
	Put(store Storer)
	Destroy(id []byte) error
	Regenerate(oldID, newID []byte) (Storer, error) // the expiration is also reset to original value
	Count() int
	NeedGC() bool
	GC()
}

Provider provider interface

type ProviderConfig

type ProviderConfig interface {
	Name() string
}

ProviderConfig provider config interface

type Session

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

Session session struct

func New

func New(cfg *Config) *Session

New return new Session

func (*Session) Destroy

func (s *Session) Destroy(ctx *fasthttp.RequestCtx) error

Destroy destroy session in fasthttp ctx

func (*Session) Get

func (s *Session) Get(ctx *fasthttp.RequestCtx) (Storer, error)

Get get user session from provider 1. get sessionID from fasthttp ctx 2. if sessionID is empty, generator sessionID and set response Set-Cookie 3. return session provider store

func (*Session) Regenerate

func (s *Session) Regenerate(ctx *fasthttp.RequestCtx) (Storer, error)

Regenerate regenerate a session id for this Storer

func (*Session) Save

func (s *Session) Save(ctx *fasthttp.RequestCtx, store Storer)

Save save the user session with current store

Use this function if you want to avoid some extra-allocations This will save the store into provider and will return it to the pool

Warning: Don't use more the store after exec this function, because, you will lose the after data For avoid it, defer this function in your request handler

func (*Session) SetProvider

func (s *Session) SetProvider(name string, cfg ProviderConfig) error

SetProvider set session provider and provider config

type Store

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

Store store

func (*Store) DataPointer

func (s *Store) DataPointer() *Dict

DataPointer get pointer of data

func (*Store) Delete

func (s *Store) Delete(key string)

Delete delete data by key

func (*Store) DeleteBytes

func (s *Store) DeleteBytes(key []byte)

DeleteBytes delete data by key

func (*Store) Flush

func (s *Store) Flush()

Flush flush all data

func (*Store) Get

func (s *Store) Get(key string) interface{}

Get get data by key

func (*Store) GetAll

func (s *Store) GetAll() Dict

GetAll get all data

func (*Store) GetBytes

func (s *Store) GetBytes(key []byte) interface{}

GetBytes get data by key

func (*Store) GetExpiration added in v1.1.0

func (s *Store) GetExpiration() time.Duration

GetExpiration get expiration for the session

func (*Store) GetSessionID

func (s *Store) GetSessionID() []byte

GetSessionID get session id

func (*Store) HasExpirationChanged added in v1.1.0

func (s *Store) HasExpirationChanged() bool

HasExpirationChanged check wether the expiration has been changed

func (*Store) Init

func (s *Store) Init(sessionID []byte, defaultExpiration time.Duration)

Init init store data and sessionID

func (*Store) Reset

func (s *Store) Reset()

Reset reset store

func (*Store) Save

func (s *Store) Save() error

Save save store

func (*Store) Set

func (s *Store) Set(key string, value interface{})

Set set data

func (*Store) SetBytes

func (s *Store) SetBytes(key []byte, value interface{})

SetBytes set data

func (*Store) SetExpiration added in v1.1.0

func (s *Store) SetExpiration(expiration time.Duration) error

SetExpiration set expiration for the session

func (*Store) SetSessionID

func (s *Store) SetSessionID(id []byte)

SetSessionID set session id

type Storer

type Storer interface {
	Save() error
	Get(key string) interface{}
	GetBytes(key []byte) interface{}
	GetAll() Dict
	Set(key string, value interface{})
	SetBytes(key []byte, value interface{})
	Delete(key string)
	DeleteBytes(key []byte)
	Flush()
	GetSessionID() []byte
	SetExpiration(expiration time.Duration) error
	GetExpiration() time.Duration
	HasExpirationChanged() bool
}

Storer session store interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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