Documentation ¶
Overview ¶
Package session implements a efficient, safely and easy-to-use session library for Go.
Example:
package main import ( "context" "fmt" "net/http" "github.com/go-session/session" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { store, err := session.Start(context.Background(), w, r) if err != nil { fmt.Fprint(w, err) return } store.Set("foo", "bar") err = store.Save() if err != nil { fmt.Fprint(w, err) return } http.Redirect(w, r, "/foo", 302) }) http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { store, err := session.Start(context.Background(), w, r) if err != nil { fmt.Fprint(w, err) return } foo, ok := store.Get("foo") if ok { fmt.Fprintf(w, "foo:%s", foo) return } fmt.Fprint(w, "does not exist") }) http.ListenAndServe(":8080", nil) }
Open in your web browser at http://localhost:8080
Output:
foo:bar
Learn more at https://github.com/go-session/session
Index ¶
- Variables
- func Destroy(ctx context.Context, w http.ResponseWriter, r *http.Request) error
- func FromReqContext(ctx context.Context) (*http.Request, bool)
- func FromResContext(ctx context.Context) (http.ResponseWriter, bool)
- func InitManager(opt ...Option)
- type Manager
- type ManagerStore
- type Option
- func SetCookieLifeTime(cookieLifeTime int) Option
- func SetCookieName(cookieName string) Option
- func SetDomain(domain string) Option
- func SetEnableSIDInHTTPHeader(enableSIDInHTTPHeader bool) Option
- func SetEnableSIDInURLQuery(enableSIDInURLQuery bool) Option
- func SetEnableSetCookie(enableSetCookie bool) Option
- func SetExpired(expired int64) Option
- func SetSecure(secure bool) Option
- func SetSessionID(sessionID func() string) Option
- func SetSessionNameInHTTPHeader(sessionNameInHTTPHeader string) Option
- func SetSign(sign []byte) Option
- func SetStore(store ManagerStore) Option
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidSessionID invalid session id ErrInvalidSessionID = errors.New("invalid session id") )
Functions ¶
func FromReqContext ¶
FromReqContext returns the Request value stored in ctx, if any.
func FromResContext ¶
func FromResContext(ctx context.Context) (http.ResponseWriter, bool)
FromResContext returns the ResponseWriter value stored in ctx, if any.
func InitManager ¶
func InitManager(opt ...Option)
InitManager initialize the global session management instance
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager A session management instance, including start and destroy operations
func NewManager ¶
NewManager Create a session management instance
type ManagerStore ¶
type ManagerStore interface { // Check the session store exists Check(ctx context.Context, sid string) (bool, error) // Create a session store and specify the expiration time (in seconds) Create(ctx context.Context, sid string, expired int64) (Store, error) // Update a session store and specify the expiration time (in seconds) Update(ctx context.Context, sid string, expired int64) (Store, error) // Delete a session store Delete(ctx context.Context, sid string) error // Use sid to replace old sid and return session store Refresh(ctx context.Context, oldsid, sid string, expired int64) (Store, error) // Close storage, release resources Close() error }
ManagerStore Management of session storage, including creation, update, and delete operations
func NewMemoryStore ¶
func NewMemoryStore() ManagerStore
NewMemoryStore create an instance of a memory store
type Option ¶
type Option func(*options)
Option A session parameter options
func SetCookieLifeTime ¶
SetCookieLifeTime Set the cookie expiration time (in seconds)
func SetEnableSIDInHTTPHeader ¶
SetEnableSIDInHTTPHeader Allow session id to be obtained from the request header
func SetEnableSIDInURLQuery ¶
SetEnableSIDInURLQuery Allow session id from URL query parameters (enabled by default)
func SetEnableSetCookie ¶
SetEnableSetCookie Enable writing session id to cookie (enabled by default, can be turned off if no cookie is written)
func SetExpired ¶
SetExpired Set session expiration time (in seconds)
func SetSessionID ¶
SetSessionID Set callback function to generate session id
func SetSessionNameInHTTPHeader ¶
SetSessionNameInHTTPHeader The key name in the request header where the session ID is stored (if it is empty, the default is the cookie name)
type Store ¶
type Store interface { // Get a session storage context Context() context.Context // Get the current session id SessionID() string // Set session value, call save function to take effect Set(key string, value interface{}) // Get session value Get(key string) (interface{}, bool) // Delete session value, call save function to take effect Delete(key string) interface{} // Save session data Save() error // Clear all session data Flush() error }
Store A session id storage operation