session

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContextEngValue is the context key for storing the session Engine.
	ContextEngValue = "HTTPOKSESSCTXENGVALUE"
	// ContextSessValue is the context key for storing the session data.
	ContextSessValue = "HTTPOKSESSCTXSESSVALUE"
	// DefaultName is the default name for session cookies.
	DefaultName = "HTTPOKSESSID"
	// DefaultPrefix is the default prefix used for session keys.
	DefaultPrefix = "httpok:session"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

type Encoder interface {
	Encode(any) ([]byte, error)
	Decode([]byte, any) error
}

Encoder is an interface for encoding and decoding session data.

type Engine

type Engine interface {
	Enabled() bool
	NewId() string
	SetEnabled(bool)
	// TODO: PurgeSession(id string)
	Name() string
	SetName(string)
	Read(string, any) error
	Start(context.Context) error
	Stop() error
	Store(string, any) error
	Purge() error
	GetSession(string, context.Context) (Session, error)
	SessionNotExists(string) (bool, error)
	StoreSession(string, Session) error
}

Engine defines the interface for session management.

func EngineFromContext

func EngineFromContext(ctx context.Context) (Engine, error)

EngineFromContext retrieves the session Engine from the context.

type EngineProperties

type EngineProperties struct {
	AgeLimit time.Duration

	Encoder
	logger.Logger

	// TODO: Add this to the interface
	Prefix        string
	PurgeDuration time.Duration
	// contains filtered or unexported fields
}

EngineProperties contains common properties for session engines.

type FileEngine

type FileEngine struct {
	EngineProperties
	Dir string
}

FileEngine implements the Engine interface for file-based session storage.

func NewFileEngine

func NewFileEngine() *FileEngine

NewFileEngine creates and returns a new FileEngine with default settings.

func (*FileEngine) Enabled

func (e *FileEngine) Enabled() bool

Enabled returns whether the session management is enabled.

func (*FileEngine) GetSession

func (e *FileEngine) GetSession(id string, ctx context.Context) (Session, error)

GetSession retrieves a session from file storage based on the given ID.

func (*FileEngine) Name

func (e *FileEngine) Name() string

Name returns the name of the session engine.

func (*FileEngine) NewId

func (e *FileEngine) NewId() string

NewId generates and returns a new session ID.

func (*FileEngine) Purge

func (e *FileEngine) Purge() error

Purge removes expired sessions from file storage.

func (*FileEngine) Read

func (e *FileEngine) Read(id string, v any) error

Read retrieves and decodes session data from file storage.

func (*FileEngine) SessionNotExists

func (e *FileEngine) SessionNotExists(id string) (bool, error)

SessionNotExists checks if a session with the given ID does not exist in file storage.

func (*FileEngine) SetEnabled

func (e *FileEngine) SetEnabled(enabled bool)

SetEnabled sets the enabled status of the session management.

func (*FileEngine) SetName

func (e *FileEngine) SetName(n string)

SetName sets the name of the session engine.

func (*FileEngine) Start

func (e *FileEngine) Start(_ context.Context) error

Start initializes the directory for session storage.

func (*FileEngine) Stop

func (e *FileEngine) Stop() error

Stop is a placeholder for stopping the session engine, currently does nothing.

func (*FileEngine) Store

func (e *FileEngine) Store(id string, v any) error

Store saves session data to file storage.

func (*FileEngine) StoreSession

func (e *FileEngine) StoreSession(id string, s Session) error

StoreSession saves the session data back to file storage.

type JsonEncoder

type JsonEncoder struct {
}

JsonEncoder implements the Encoder interface using JSON serialization.

func (*JsonEncoder) Decode

func (e *JsonEncoder) Decode(data []byte, v any) error

Decode deserializes the JSON data into the provided value.

func (*JsonEncoder) Encode

func (e *JsonEncoder) Encode(v any) ([]byte, error)

Encode serializes the given value into JSON.

type RedisEngine

type RedisEngine struct {
	EngineProperties
	// contains filtered or unexported fields
}

RedisEngine is the implementation of session management using Redis.

func NewRedisEngine

func NewRedisEngine() (*RedisEngine, error)

NewRedisEngine creates and returns a new RedisEngine with default settings.

func (*RedisEngine) Enabled

func (e *RedisEngine) Enabled() bool

Enabled returns whether the session management is enabled.

func (*RedisEngine) GetSession

func (e *RedisEngine) GetSession(id string, ctx context.Context) (Session, error)

GetSession retrieves a session from Redis based on the given ID.

func (*RedisEngine) Name

func (e *RedisEngine) Name() string

Name returns the name of the session engine.

func (*RedisEngine) NewId

func (e *RedisEngine) NewId() string

NewId generates and returns a new session ID.

func (*RedisEngine) Purge

func (e *RedisEngine) Purge() error

Purge removes expired sessions from Redis.

func (*RedisEngine) Read

func (e *RedisEngine) Read(id string, v any) error

Read retrieves and decodes session data from Redis.

func (*RedisEngine) SessionNotExists

func (e *RedisEngine) SessionNotExists(id string) (bool, error)

SessionNotExists checks if a session with the given ID does not exist in Redis.

func (*RedisEngine) SetEnabled

func (e *RedisEngine) SetEnabled(enabled bool)

SetEnabled sets the enabled status of the session management.

func (*RedisEngine) SetName

func (e *RedisEngine) SetName(n string)

SetName sets the name of the session engine.

func (*RedisEngine) Start

func (e *RedisEngine) Start(ctx context.Context) error

Start initializes the connection to Redis.

func (*RedisEngine) Stop

func (e *RedisEngine) Stop() error

Stop is a placeholder for stopping the session engine, currently does nothing.

func (*RedisEngine) Store

func (e *RedisEngine) Store(id string, v any) error

Store saves session data to Redis.

func (*RedisEngine) StoreSession

func (e *RedisEngine) StoreSession(id string, s Session) error

StoreSession saves the session data back to Redis.

type Session

type Session struct {
	Id        string
	Changed   bool
	Ctx       context.Context
	Data      map[string]any
	Destroyed bool
	Params    any
}

Session represents a user session with data storage capabilities.

func SessionFromContext

func SessionFromContext(ctx context.Context) (*Session, error)

SessionFromContext retrieves the session from the context.

func (*Session) Clear

func (s *Session) Clear()

Clear removes all data from the session and marks it as changed.

func (*Session) Delete

func (s *Session) Delete(key string) error

Delete removes a key-value pair from the session data.

func (*Session) Destroy

func (s *Session) Destroy() error

Destroy clears the session data and marks it as destroyed.

func (*Session) Get

func (s *Session) Get(key string) (any, error)

Get retrieves a value from the session by key.

func (*Session) Has

func (s *Session) Has(key string) (bool, error)

Has checks if a key exists in the session data.

func (*Session) Set

func (s *Session) Set(key string, value any) error

Set adds or updates a key-value pair in the session data.

Jump to

Keyboard shortcuts

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