session2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

This code is based on https://github.com/icza/session. Several simplification rounds have been applied, aided by Claude Code.

Documentation

Overview

Package session provides easy-to-use, secure HTTP session management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(sess *Session, w http.ResponseWriter)

Add stores sess and sets the session cookie on w.

func Close

func Close()

Close stops the background cleaner goroutine.

func Init

func Init(o Options)

Init closes the current global manager and replaces it with a new one configured by o. Use this at startup or in tests to override defaults.

func Len

func Len() int

Len returns the number of active sessions in the global manager.

func Reinit

func Reinit()

func Remove

func Remove(sess *Session, w http.ResponseWriter)

Remove deletes sess and instructs the client to clear its cookie.

Types

type Options

type Options struct {
	// Name of the session ID cookie. Default: "sessid".
	CookieName string
	// Allow session cookies over plain HTTP. Default: false (HTTPS only).
	AllowHTTP bool
	// Max age of the session cookie. Default: 30 days.
	// Set to a negative value for a session-scoped cookie (expires when browser closes).
	CookieMaxAge time.Duration
	// Cookie path. Default: "/".
	CookiePath string
	// How often the store is checked for expired sessions. Default: 10s.
	CleanInterval time.Duration
}

Options configures the session manager. All fields are optional.

type SessOptions

type SessOptions struct {
	// Initial attributes stored in the session.
	Attrs map[string]interface{}
	// Session timeout. Default: 30 minutes.
	Timeout time.Duration
}

SessOptions defines options for NewSession. All fields are optional.

type Session

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

Session holds the data for a single HTTP session. All methods are safe for concurrent use.

func Get

func Get(r *http.Request) *Session

Get returns the session associated with r, or nil if none exists.

func NewSession

func NewSession(o ...SessOptions) *Session

NewSession creates a new Session. Pass a SessOptions value to override defaults.

func (*Session) Accessed

func (s *Session) Accessed() time.Time

Accessed returns the time the session was last accessed.

func (*Session) Attr

func (s *Session) Attr(name string) interface{}

Attr returns the value of a stored attribute, or nil if not set.

func (*Session) Attrs

func (s *Session) Attrs() map[string]interface{}

Attrs returns a copy of all attributes.

func (*Session) Created

func (s *Session) Created() time.Time

Created returns the time the session was created.

func (*Session) ID

func (s *Session) ID() string

ID returns the session identifier.

func (*Session) SetAttr

func (s *Session) SetAttr(name string, value interface{})

SetAttr sets an attribute value. Pass nil to delete the attribute.

func (*Session) Timeout

func (s *Session) Timeout() time.Duration

Timeout returns the inactivity duration after which the session expires.

Jump to

Keyboard shortcuts

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