# README
session
A efficient, safely and easy-to-use session library for Go.
Quick Start
Download and install
go get -v github.com/Scumfunk/session/v3
Create file server.go
package main
import (
"context"
"fmt"
"net/http"
session "github.com/Scumfunk/session/v3"
)
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)
}
Build and run
go build server.go
./server
Open in your web browser
foo:bar
Features
- Easy to use
- Multi-storage support
- Multi-middleware support
- More secure, signature-based tamper-proof
- Context support
- Support request header and query parameters
Store Implementations
- https://github.com/go-session/redis - Redis
- https://github.com/go-session/mongo - MongoDB
- https://github.com/go-session/gorm - GORM
- https://github.com/go-session/mysql - MySQL
- https://github.com/go-session/buntdb - BuntDB
- https://github.com/go-session/cookie - Cookie
Middlewares
- https://github.com/go-session/gin-session - Gin
- https://github.com/go-session/beego-session - Beego
- https://github.com/go-session/gear-session - Gear
- https://github.com/go-session/echo-session - Echo
MIT License
Copyright (c) 2021 Lyric
# Functions
Destroy a session.
FromReqContext returns the Request value stored in ctx, if any.
FromResContext returns the ResponseWriter value stored in ctx, if any.
InitManager initialize the global session management instance.
NewManager Create a session management instance.
NewMemoryStore create an instance of a memory store.
Refresh a session and return to session storage.
SetCookieLifeTime Set the cookie expiration time (in seconds).
SetCookieName Set the cookie name.
SetDomain Set the domain name of the cookie.
SetEnableSetCookie Enable writing session id to cookie (enabled by default, can be turned off if no cookie is written).
SetEnableSIDInHTTPHeader Allow session id to be obtained from the request header.
SetEnableSIDInURLQuery Allow session id from URL query parameters (enabled by default).
SetExpired Set session expiration time (in seconds).
SetForceSecure Set cookie security (force).
SetSameSite Set SameSite attribute of the cookie.
SetSecure Set cookie security.
SetSessionID Set callback function to generate session id.
SetSessionNameInHTTPHeader The key name in the request header where the session ID is stored (if it is empty, the default is the cookie name).
SetSign Set the session id signature value.
SetStore Set session management storage.
Start a session and return to session storage.
# Constants
Version # of session.
# Variables
ErrInvalidSessionID invalid session id.
# Interfaces
ManagerStore Management of session storage, including creation, update, and delete operations.
Store A session id storage operation.
# Type aliases
IDHandlerFunc Define the handler to get the session id.
Option A session parameter options.