session

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: EUPL-1.2 Imports: 12 Imported by: 0

Documentation

Overview

Package session provides cookie-based session management as a burrow contrib app.

Reading and Writing Values

Use GetString and GetInt64 to read typed values from the session. Use Set to write a single key-value pair (the cookie is saved immediately). Use Save to replace all session values at once, and Clear to delete the session.

All read/write functions require the session middleware to be active.

Testing

Use Inject to set up session state in tests without the full middleware. It returns a new request with the session values available via the standard getters.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear(w http.ResponseWriter, r *http.Request)

Clear clears the session and writes a deletion cookie.

func Delete

func Delete(w http.ResponseWriter, r *http.Request, key string) error

Delete removes a key from the session and immediately writes the cookie.

func GetInt64

func GetInt64(r *http.Request, key string) int64

GetInt64 retrieves an int64 value from the session.

func GetString

func GetString(r *http.Request, key string) string

GetString retrieves a string value from the session.

func GetValues

func GetValues(r *http.Request) map[string]any

GetValues retrieves all session values from the request. Returns nil if no session is active. The returned map is a shallow copy; mutations do not affect the session — use Set to persist changes.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/oliverandrich/burrow/contrib/session"
)

func main() {
	r := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
	r = session.Inject(r, map[string]any{
		"theme": "dark",
	})

	values := session.GetValues(r)
	fmt.Println(values["theme"])
}
Output:

dark

func Inject

func Inject(r *http.Request, values map[string]any) *http.Request

Inject sets up session state in the request context without the full middleware. This is intended for use in tests by other packages. It returns a new request with the session state injected.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/oliverandrich/burrow/contrib/session"
)

func main() {
	r := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
	r = session.Inject(r, map[string]any{
		"username": "alice",
		"visits":   int64(42),
	})

	fmt.Println(session.GetString(r, "username"))
	fmt.Println(session.GetInt64(r, "visits"))
}
Output:

alice
42

func Save

func Save(w http.ResponseWriter, r *http.Request, values map[string]any) error

Save replaces all session values and immediately writes the cookie. Use this for operations like login where you want a fresh session.

func Set

func Set(w http.ResponseWriter, r *http.Request, key string, value any) error

Set sets a single value in the session and immediately writes the cookie.

Types

type App

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

App implements the session contrib app.

func New

func New() *App

New creates a new session app.

func (*App) Configure

func (a *App) Configure(cmd *cli.Command) error

func (*App) Flags

func (a *App) Flags(configSource func(key string) cli.ValueSource) []cli.Flag

func (*App) Manager

func (a *App) Manager() *Manager

Manager returns the session manager for other apps to use.

func (*App) Middleware

func (a *App) Middleware() []func(http.Handler) http.Handler

func (*App) Name

func (a *App) Name() string

func (*App) Register

func (a *App) Register(cfg *burrow.AppConfig) error

type Manager

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

Manager handles session cookie creation and parsing.

func (*Manager) Clear

func (m *Manager) Clear() *http.Cookie

Clear returns a cookie that clears the session.

func (*Manager) Parse

func (m *Manager) Parse(r *http.Request) (map[string]any, error)

Parse parses the session cookie from the request. Returns nil, nil if no valid session is present.

func (*Manager) Save

func (m *Manager) Save(values map[string]any) (*http.Cookie, error)

Save creates a new session cookie with the given values.

Jump to

Keyboard shortcuts

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