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 ¶
- func Clear(w http.ResponseWriter, r *http.Request)
- func Delete(w http.ResponseWriter, r *http.Request, key string) error
- func GetInt64(r *http.Request, key string) int64
- func GetString(r *http.Request, key string) string
- func GetValues(r *http.Request) map[string]any
- func Inject(r *http.Request, values map[string]any) *http.Request
- func Save(w http.ResponseWriter, r *http.Request, values map[string]any) error
- func Set(w http.ResponseWriter, r *http.Request, key string, value any) error
- type App
- type Manager
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 GetValues ¶
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 ¶
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
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App implements the session contrib app.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles session cookie creation and parsing.