session

package module
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 16 Imported by: 3

README

session

GitHub Workflow Status Codecov GoDoc Sourcegraph

Package session is a middleware that provides the session management for Flamego.

Installation

The minimum requirement of Go is 1.18.

go get github.com/flamego/session

Getting started

package main

import (
	"github.com/flamego/flamego"
	"github.com/flamego/session"
)

func main() {
	f := flamego.Classic()
	f.Use(session.Sessioner())
	f.Get("/", func(s session.Session) {
		s.Set("user_id", 123)
		userID, ok := s.Get("user_id").(int)
		// ...
	})
	f.Run()
}

Getting help

License

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMinimumSIDLength = errors.Errorf("the SID does not have the minimum required length %d", minimumSIDLength)

Functions

func GobEncoder

func GobEncoder(data Data) ([]byte, error)

GobEncoder is a session data encoder using Gob.

func Sessioner

func Sessioner(opts ...Options) flamego.Handler

Sessioner returns a middleware handler that injects session.Session and session.Store into the request context, which are used for manipulating session data.

Types

type BaseSession

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

BaseSession implements basic operations for the session data.

func NewBaseSession

func NewBaseSession(sid string, encoder Encoder) *BaseSession

NewBaseSession returns a new BaseSession with given session ID.

func NewBaseSessionWithData added in v1.5.1

func NewBaseSessionWithData(sid string, encoder Encoder, data Data) *BaseSession

NewBaseSessionWithData returns a new BaseSession with given session ID and initial data.

func (*BaseSession) Delete

func (s *BaseSession) Delete(key interface{})

func (*BaseSession) Encode

func (s *BaseSession) Encode() ([]byte, error)

func (*BaseSession) Flush

func (s *BaseSession) Flush()

func (*BaseSession) Get

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

func (*BaseSession) HasChanged added in v1.6.1

func (s *BaseSession) HasChanged() bool

func (*BaseSession) ID

func (s *BaseSession) ID() string

func (*BaseSession) Set

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

func (*BaseSession) SetFlash

func (s *BaseSession) SetFlash(val interface{})

type CookieOptions

type CookieOptions struct {
	// Name is the name of the cookie. Default is "flamego_session".
	Name string
	// Path is the Path attribute of the cookie. Default is "/".
	Path string
	// Domain is the Domain attribute of the cookie. Default is not set.
	Domain string
	// MaxAge is the MaxAge attribute of the cookie. Default is not set.
	MaxAge int
	// Secure specifies whether to set Secure for the cookie.
	Secure bool
	// HTTPOnly specifies whether to set HTTPOnly for the cookie.
	HTTPOnly bool
	// SameSite is the SameSite attribute of the cookie. Default is
	// http.SameSiteLaxMode.
	SameSite http.SameSite
}

CookieOptions contains options for setting HTTP cookies.

type Data

type Data map[interface{}]interface{}

Data is the data structure for storing session data.

func GobDecoder

func GobDecoder(binary []byte) (Data, error)

GobDecoder is a session data decoder using Gob.

type Decoder

type Decoder func([]byte) (Data, error)

Decoder is a decoder to decode binary to session data.

type Encoder

type Encoder func(Data) ([]byte, error)

Encoder is an encoder to encode session data to binary.

type FileConfig

type FileConfig struct {

	// Lifetime is the duration to have no access to a session before being
	// recycled. Default is 3600 seconds.
	Lifetime time.Duration
	// RootDir is the root directory of file session items stored on the local file
	// system. Default is "sessions".
	RootDir string
	// Encoder is the encoder to encode session data. Default is GobEncoder.
	Encoder Encoder
	// Decoder is the decoder to decode session data. Default is GobDecoder.
	Decoder Decoder
	// contains filtered or unexported fields
}

FileConfig contains options for the file session store.

type Flash

type Flash interface{}

Flash is anything that gets retrieved and deleted as soon as the next request happens.

type Initer

type Initer func(ctx context.Context, args ...interface{}) (Store, error)

Initer takes arbitrary number of arguments needed for initialization and returns an initialized session store.

func FileIniter

func FileIniter() Initer

FileIniter returns the Initer for the file session store.

func MemoryIniter

func MemoryIniter() Initer

MemoryIniter returns the Initer for the memory session store.

type MemoryConfig

type MemoryConfig struct {

	// Lifetime is the duration to have no access to a session before being
	// recycled. Default is 3600 seconds.
	Lifetime time.Duration
	// contains filtered or unexported fields
}

MemoryConfig contains options for the memory session store.

type Options

type Options struct {
	// Initer is the initialization function of the session store. Default is
	// session.MemoryIniter.
	Initer Initer
	// Config is the configuration object to be passed to the Initer for the session
	// store.
	Config interface{}
	// Cookie is a set of options for setting HTTP cookies.
	Cookie CookieOptions
	// IDLength specifies the length of session IDs. Default is 16.
	IDLength int
	// GCInterval is the time interval for GC operations. Default is 5 minutes.
	GCInterval time.Duration
	// ErrorFunc is the function used to print errors when something went wrong on
	// the background. Default is to drop errors silently.
	ErrorFunc func(err error)
	// ReadIDFunc is the function to read session ID from the request. Default is
	// reading from cookie.
	ReadIDFunc func(r *http.Request) string
	// WriteIDFunc is the function to write session ID to the response. Default is
	// writing to cookie. The `created` argument indicates whether a new session was
	// created in the session store.
	WriteIDFunc func(w http.ResponseWriter, r *http.Request, sid string, created bool)
}

Options contains options for the session.Sessioner middleware.

type Session

type Session interface {
	// ID returns the session ID.
	ID() string
	// Get returns the value of given key in the session. It returns nil if no such
	// key exists.
	Get(key interface{}) interface{}
	// Set sets the value of given key in the session.
	Set(key, val interface{})
	// SetFlash sets the flash to be the given value in the session.
	SetFlash(val interface{})
	// Delete deletes a key from the session.
	Delete(key interface{})
	// Flush wipes out all existing data in the session.
	Flush()
	// Encode encodes session data to binary.
	Encode() ([]byte, error)
	// HasChanged returns whether the session has changed.
	HasChanged() bool
}

Session is a session for the current request.

type Store

type Store interface {
	// Exist returns true of the session with given ID exists.
	Exist(ctx context.Context, sid string) bool
	// Read returns the session with given ID. If a session with the ID does not
	// exist, a new session with the same ID is created and returned.
	Read(ctx context.Context, sid string) (Session, error)
	// Destroy deletes session with given ID from the session store completely.
	Destroy(ctx context.Context, sid string) error
	// Touch updates the expiry time of the session with given ID. It does nothing
	// if there is no session associated with the ID.
	Touch(ctx context.Context, sid string) error
	// Save persists session data to the session store.
	Save(ctx context.Context, session Session) error
	// GC performs a GC operation on the session store.
	GC(ctx context.Context) error
}

Store is a session store with capabilities of checking, reading, destroying and GC sessions.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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