session

package
v2.0.0-beta1+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2015 License: Apache-2.0, Apache-2.0 Imports: 18 Imported by: 0

README

session Build Status

Middleware session provides session management for Macaron. It can use many session providers, including memory, file, Redis, Memcache, PostgreSQL, MySQL, Couchbase and Ledis.

Installation
go get github.com/macaron-contrib/session

Getting Help

License

This project is under Apache v2 License. See the LICENSE file for the full license text.

Documentation

Overview

Package session a middleware that provides the session manager of Macaron.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeGob

func DecodeGob(encoded []byte) (map[interface{}]interface{}, error)

func EncodeGob

func EncodeGob(obj map[interface{}]interface{}) ([]byte, error)

func Register

func Register(name string, provider Provider)

Register registers a provider.

func Sessioner

func Sessioner(options ...Options) macaron.Handler

Sessioner is a middleware that maps a session.SessionStore service into the Macaron handler chain. An single variadic session.Options struct can be optionally provided to configure.

func Version

func Version() string

Types

type FileProvider

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

FileProvider represents a file session provider implementation.

func (*FileProvider) Count

func (p *FileProvider) Count() int

Count counts and returns number of sessions.

func (*FileProvider) Destory

func (p *FileProvider) Destory(sid string) error

Destory deletes a session by session ID.

func (*FileProvider) Exist

func (p *FileProvider) Exist(sid string) bool

Exist returns true if session with given ID exists.

func (*FileProvider) GC

func (p *FileProvider) GC()

GC calls GC to clean expired sessions.

func (*FileProvider) Init

func (p *FileProvider) Init(maxlifetime int64, rootPath string) error

Init initializes file session provider with given root path.

func (*FileProvider) Read

func (p *FileProvider) Read(sid string) (_ RawStore, err error)

Read returns raw session store by session ID.

func (*FileProvider) Regenerate

func (p *FileProvider) Regenerate(oldsid, sid string) (_ RawStore, err error)

Regenerate regenerates a session store from old session ID to new one.

type FileSessionStore

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

FileSessionStore represents a file session store implementation.

func NewFileSessionStore

func NewFileSessionStore(p *FileProvider, sid string, kv map[interface{}]interface{}) *FileSessionStore

NewFileSessionStore creates and returns a file session store.

func (*FileSessionStore) Delete

func (s *FileSessionStore) Delete(key interface{}) error

Delete delete a key from session.

func (*FileSessionStore) Flush

func (s *FileSessionStore) Flush() error

Flush deletes all session data.

func (*FileSessionStore) Get

func (s *FileSessionStore) Get(key interface{}) interface{}

Get gets value by given key in session.

func (*FileSessionStore) ID

func (s *FileSessionStore) ID() string

ID returns current session ID.

func (*FileSessionStore) Release

func (s *FileSessionStore) Release() error

Release releases resource and save data to provider.

func (*FileSessionStore) Set

func (s *FileSessionStore) Set(key, val interface{}) error

Set sets value to given key in session.

type Flash

type Flash struct {
	url.Values
	ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
	// contains filtered or unexported fields
}

func (*Flash) Error

func (f *Flash) Error(msg string, current ...bool)

func (*Flash) Info

func (f *Flash) Info(msg string, current ...bool)

func (*Flash) Success

func (f *Flash) Success(msg string, current ...bool)

func (*Flash) Warning

func (f *Flash) Warning(msg string, current ...bool)

type Manager

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

Manager represents a struct that contains session provider and its configuration.

func NewManager

func NewManager(name string, opt Options) (*Manager, error)

NewManager creates and returns a new session manager by given provider name and configuration. It panics when given provider isn't registered.

func (*Manager) Count

func (m *Manager) Count() int

Count counts and returns number of sessions.

func (*Manager) Destory

func (m *Manager) Destory(ctx *macaron.Context) error

Destory deletes a session by given ID.

func (*Manager) GC

func (m *Manager) GC()

GC starts GC job in a certain period.

func (*Manager) Read

func (m *Manager) Read(sid string) (RawStore, error)

Read returns raw session store by session ID.

func (*Manager) RegenerateId

func (m *Manager) RegenerateId(ctx *macaron.Context) (sess RawStore, err error)

RegenerateId regenerates a session store from old session ID to new one.

func (*Manager) SetSecure

func (m *Manager) SetSecure(secure bool)

SetSecure indicates whether to set cookie with HTTPS or not.

func (*Manager) Start

func (m *Manager) Start(ctx *macaron.Context) (RawStore, error)

Start starts a session by generating new one or retrieve existence one by reading session ID from HTTP request if it's valid.

type MemProvider

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

MemProvider represents a in-memory session provider implementation.

func (*MemProvider) Count

func (p *MemProvider) Count() int

Count counts and returns number of sessions.

func (*MemProvider) Destory

func (p *MemProvider) Destory(sid string) error

Destory deletes a session by session ID.

func (*MemProvider) Exist

func (p *MemProvider) Exist(sid string) bool

Exist returns true if session with given ID exists.

func (*MemProvider) GC

func (p *MemProvider) GC()

GC calls GC to clean expired sessions.

func (*MemProvider) Init

func (p *MemProvider) Init(maxLifetime int64, _ string) error

Init initializes memory session provider.

func (*MemProvider) Read

func (p *MemProvider) Read(sid string) (_ RawStore, err error)

Read returns raw session store by session ID.

func (*MemProvider) Regenerate

func (p *MemProvider) Regenerate(oldsid, sid string) (RawStore, error)

Regenerate regenerates a session store from old session ID to new one.

type MemSessionStore

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

MemSessionStore represents a in-memory session store implementation.

func NewMemSessionStore

func NewMemSessionStore(sid string) *MemSessionStore

NewMemSessionStore creates and returns a memory session store.

func (*MemSessionStore) Delete

func (s *MemSessionStore) Delete(key interface{}) error

Delete delete a key from session.

func (*MemSessionStore) Flush

func (s *MemSessionStore) Flush() error

Flush deletes all session data.

func (*MemSessionStore) Get

func (s *MemSessionStore) Get(key interface{}) interface{}

Get gets value by given key in session.

func (*MemSessionStore) ID

func (s *MemSessionStore) ID() string

ID returns current session ID.

func (*MemSessionStore) Release

func (_ *MemSessionStore) Release() error

Release releases resource and save data to provider.

func (*MemSessionStore) Set

func (s *MemSessionStore) Set(key, val interface{}) error

Set sets value to given key in session.

type Options

type Options struct {
	// Name of provider. Default is "memory".
	Provider string
	// Provider configuration, it's corresponding to provider.
	ProviderConfig string
	// Cookie name to save session ID. Default is "MacaronSession".
	CookieName string
	// Cookie path to store. Default is "/".
	CookiePath string
	// GC interval time in seconds. Default is 3600.
	Gclifetime int64
	// Max life time in seconds. Default is whatever GC interval time is.
	Maxlifetime int64
	// Use HTTPS only. Default is false.
	Secure bool
	// Cookie life time. Default is 0.
	CookieLifeTime int
	// Cookie domain name. Default is empty.
	Domain string
	// Session ID length. Default is 16.
	IDLength int
	// Configuration section name. Default is "session".
	Section string
}

Options represents a struct for specifying configuration options for the session middleware.

type Provider

type Provider interface {
	// Init initializes session provider.
	Init(gclifetime int64, config string) error
	// Read returns raw session store by session ID.
	Read(sid string) (RawStore, error)
	// Exist returns true if session with given ID exists.
	Exist(sid string) bool
	// Destory deletes a session by session ID.
	Destory(sid string) error
	// Regenerate regenerates a session store from old session ID to new one.
	Regenerate(oldsid, sid string) (RawStore, error)
	// Count counts and returns number of sessions.
	Count() int
	// GC calls GC to clean expired sessions.
	GC()
}

Provider is the interface that provides session manipulations.

type RawStore

type RawStore interface {
	// Set sets value to given key in session.
	Set(key, value interface{}) error
	// Get gets value by given key in session.
	Get(key interface{}) interface{}
	// Delete delete a key from session.
	Delete(key interface{}) error
	// ID returns current session ID.
	ID() string
	// Release releases session resource and save data to provider.
	Release() error
	// Flush deletes all session data.
	Flush() error
}

RawStore is the interface that operates the session data.

type Store

type Store interface {
	RawStore
	// Read returns raw session store by session ID.
	Read(sid string) (RawStore, error)
	// Destory deletes a session.
	Destory(*macaron.Context) error
	// RegenerateId regenerates a session store from old session ID to new one.
	RegenerateId(*macaron.Context) (RawStore, error)
	// Count counts and returns number of sessions.
	Count() int
	// GC calls GC to clean expired sessions.
	GC()
}

Store is the interface that contains all data for one session process with specific ID.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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