Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

README

Session

Middleware session provides session management which based on a fork of a fork of Macaron Session for go-chi. It can use many session providers, including memory, file, Redis, Memcache, PostgreSQL and MySQL.

Installation

go get code.forgejo.org/go-chi/session

Credits

This package is a modified version of go-macaron/session.

License

This project is under the Apache License, Version 2.0. See the LICENSE file for the full license text.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeGob

func DecodeGob(encoded []byte) (out map[any]any, err error)

DecodeGob decodes bytes to obj

func EncodeGob

func EncodeGob(obj map[any]any) ([]byte, error)

EncodeGob encodes obj with gob

func GetCookie

func GetCookie(req *http.Request, name string) string

GetCookie returns given cookie value from request header.

func Register

func Register(name string, provider Provider)

Register registers a provider.

func RegisterFn

func RegisterFn(name string, providerfn func() Provider)

RegisterFn registers a provider function.

func Sessioner

func Sessioner(options ...Options) func(next http.Handler) http.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.

Types

type DeferredResponseWriter added in v1.0.3

type DeferredResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*DeferredResponseWriter) Write added in v1.0.3

func (w *DeferredResponseWriter) Write(b []byte) (int, error)

func (*DeferredResponseWriter) WriteHeader added in v1.0.3

func (w *DeferredResponseWriter) WriteHeader(statusCode int)

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

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

Destroy 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 FileStore

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

FileStore represents a file session store implementation.

func NewFileStore

func NewFileStore(p *FileProvider, sid string, kv map[any]any) *FileStore

NewFileStore creates and returns a file session store.

func (*FileStore) Delete

func (s *FileStore) Delete(key any) error

Delete delete a key from session.

func (*FileStore) Empty added in v1.0.3

func (s *FileStore) Empty() bool

True if no keys have been set

func (*FileStore) Flush

func (s *FileStore) Flush() error

Flush deletes all session data.

func (*FileStore) Get

func (s *FileStore) Get(key any) any

Get gets value by given key in session.

func (*FileStore) ID

func (s *FileStore) ID() string

ID returns current session ID.

func (*FileStore) Release

func (s *FileStore) Release() error

Release releases resource and save data to provider.

func (*FileStore) Set

func (s *FileStore) Set(key, val any) error

Set sets value to given key in session.

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 returns an error when requested provider name isn't registered.

func (*Manager) Count

func (m *Manager) Count() int

Count counts and returns number of sessions.

func (*Manager) Destroy

func (m *Manager) Destroy(resp http.ResponseWriter, req *http.Request) error

Destroy 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(resp http.ResponseWriter, req *http.Request) (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(resp http.ResponseWriter, req *http.Request) (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) Destroy

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

Destroy 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 MemStore

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

MemStore represents a in-memory session store implementation.

func NewMemStore

func NewMemStore(sid string) *MemStore

NewMemStore creates and returns a memory session store.

func (*MemStore) Delete

func (s *MemStore) Delete(key any) error

Delete deletes a key from session.

func (*MemStore) Empty added in v1.0.3

func (s *MemStore) Empty() bool

True if no keys have been set

func (*MemStore) Flush

func (s *MemStore) Flush() error

Flush deletes all session data.

func (*MemStore) Get

func (s *MemStore) Get(key any) any

Get gets value by given key in session.

func (*MemStore) ID

func (s *MemStore) ID() string

ID returns current session ID.

func (*MemStore) Release

func (*MemStore) Release() error

Release releases resource and save data to provider.

func (*MemStore) Set

func (s *MemStore) Set(key, val any) 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
	// SameSite set the cookie SameSite
	SameSite http.SameSite
	// Cookie domain name. Default is empty.
	Domain string
	// Session ID length. Default is 16.
	IDLength int
	// Ignore release for websocket. Default is false.
	IgnoreReleaseForWebSocket bool
	// Defer Set-Cookie to avoid session cookies for empty sessions, see
	// comment before deferredSessionHandler()
	DeferSetCookie bool
}

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

func PrepareOptions

func PrepareOptions(options []Options) Options

PrepareOptions gives some default values for options

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
	// Destroy deletes a session by session ID.
	Destroy(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(any, any) error
	// Get gets value by given key in session.
	Get(any) any
	// Delete deletes a key from session.
	Delete(any) 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
	// Empty returns true if no keys have been set
	Empty() bool
}

RawStore is the interface that operates the session data.

type Store

type Store interface {
	RawStore
	// Read returns raw session store by session ID.
	Read(string) (RawStore, error)
	// Destroy deletes a session.
	Destroy(http.ResponseWriter, *http.Request) error
	// RegenerateID regenerates a session store from old session ID to new one.
	RegenerateID(http.ResponseWriter, *http.Request) (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.

func GetSession

func GetSession(req *http.Request) Store

GetSession returns session store

func RegenerateSession

func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error)

RegenerateSession

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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