Fast http sessions manager for Go.
Simple API, while providing robust set of features such as immutability, expiration time (can be shifted), databases like badger and redis as back-end storage.
Quick view
import "github.com/kataras/go-sessions/v3"
sess := sessions.Start(http.ResponseWriter, *http.Request)
sess.
ID() string
Get(string) interface{}
HasFlash() bool
GetFlash(string) interface{}
GetFlashString(string) string
GetString(key string) string
GetInt(key string) (int, error)
GetInt64(key string) (int64, error)
GetFloat32(key string) (float32, error)
GetFloat64(key string) (float64, error)
GetBoolean(key string) (bool, error)
GetAll() map[string]interface{}
GetFlashes() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
SetImmutable(key string, value interface{})
SetFlash(string, interface{})
Delete(string)
Clear()
ClearFlashes()
Installation
The only requirement is the Go Programming Language, at least 1.14.
$ go get github.com/kataras/go-sessions/v3
go.mod
module your_app
go 1.19
require (
github.com/kataras/go-sessions/v3 v3.3.1
)
Features
Documentation
Take a look at the ./examples folder.
Outline
Start(w http.ResponseWriter,r *http.Request) Session
ShiftExpiration(w http.ResponseWriter, r *http.Request)
UpdateExpiration(w http.ResponseWriter, r *http.Request, expires time.Duration)
Destroy(w http.ResponseWriter,r *http.Request)
StartFasthttp(ctx *fasthttp.RequestCtx) Session
ShiftExpirationFasthttp(ctx *fasthttp.RequestCtx)
UpdateExpirationFasthttp(ctx *fasthttp.RequestCtx, expires time.Duration)
DestroyFasthttp(ctx *fasthttp.RequestCtx)
DestroyByID(string)
DestroyAll()
UseDatabase(Database)
Configuration
Config struct {
Cookie string
CookieSecureTLS bool
AllowReclaim bool
Encode func(cookieName string, value interface{}) (string, error)
Decode func(cookieName string, cookieValue string, v interface{}) error
Encoding Encoding
Expires time.Duration
SessionIDGenerator func() string
DisableSubdomainPersistence bool
}
Usage NET/HTTP
Start
returns a Session
, Session outline
ID() string
Get(string) interface{}
HasFlash() bool
GetFlash(string) interface{}
GetString(key string) string
GetFlashString(string) string
GetInt(key string) (int, error)
GetInt64(key string) (int64, error)
GetFloat32(key string) (float32, error)
GetFloat64(key string) (float64, error)
GetBoolean(key string) (bool, error)
GetAll() map[string]interface{}
GetFlashes() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
SetImmutable(key string, value interface{})
SetFlash(string, interface{})
Delete(string)
Clear()
ClearFlashes()
package main
import (
"fmt"
"net/http"
"time"
"github.com/kataras/go-sessions/v3"
)
type businessModel struct {
Name string
}
func main() {
app := http.NewServeMux()
sess := sessions.New(sessions.Config{
Cookie: "mysessionid",
Expires: time.Hour * 2,
DisableSubdomainPersistence: false,
})
app.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("You should navigate to the /set, /get, /delete, /clear,/destroy instead")))
})
app.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
s := sess.Start(w, r)
s.Set("name", "iris")
w.Write([]byte(fmt.Sprintf("All ok session setted to: %s", s.GetString("name"))))
})
app.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
name := sess.Start(w, r).GetString("name")
w.Write([]byte(fmt.Sprintf("The name on the /set was: %s", name)))
})
app.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) {
sess.Start(w, r).Delete("name")
})
app.HandleFunc("/clear", func(w http.ResponseWriter, r *http.Request) {
sess.Start(w, r).Clear()
})
app.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
sess.ShiftExpiration(w, r)
})
app.HandleFunc("/destroy", func(w http.ResponseWriter, r *http.Request) {
sess.Destroy(w, r)
})
app.HandleFunc("/set_immutable", func(w http.ResponseWriter, r *http.Request) {
business := []businessModel{{Name: "Edward"}, {Name: "value 2"}}
s := sess.Start(w, r)
s.SetImmutable("businessEdit", business)
businessGet := s.Get("businessEdit").([]businessModel)
businessGet[0].Name = "Gabriel"
})
app.HandleFunc("/get_immutable", func(w http.ResponseWriter, r *http.Request) {
valSlice := sess.Start(w, r).Get("businessEdit")
if valSlice == nil {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Write([]byte("please navigate to the <a href='/set_immutable'>/set_immutable</a> first"))
return
}
firstModel := valSlice.([]businessModel)[0]
if firstModel.Name != "Edward" {
panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable")
}
w.Write([]byte(fmt.Sprintf("[]businessModel[0].Name remains: %s", firstModel.Name)))
})
http.ListenAndServe(":8080", app)
}
Usage FASTHTTP
StartFasthttp
returns the same object as Start
, Session
.
ID() string
Get(string) interface{}
HasFlash() bool
GetFlash(string) interface{}
GetString(key string) string
GetFlashString(string) string
GetInt(key string) (int, error)
GetInt64(key string) (int64, error)
GetFloat32(key string) (float32, error)
GetFloat64(key string) (float64, error)
GetBoolean(key string) (bool, error)
GetAll() map[string]interface{}
GetFlashes() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
SetImmutable(key string, value interface{})
SetFlash(string, interface{})
Delete(string)
Clear()
ClearFlashes()
We have only one simple example because the API is the same, the returned session is the same for both net/http and valyala/fasthttp.
Just append the word "Fasthttp", the rest of the API remains as it's with net/http.
Start
for net/http, StartFasthttp
for valyala/fasthttp.
ShiftExpiration
for net/http, ShiftExpirationFasthttp
for valyala/fasthttp.
UpdateExpiration
for net/http, UpdateExpirationFasthttp
for valyala/fasthttp.
Destroy
for net/http, DestroyFasthttp
for valyala/fasthttp.
package main
import (
"fmt"
"github.com/kataras/go-sessions/v3"
"github.com/valyala/fasthttp"
)
func main() {
setHandler := func(reqCtx *fasthttp.RequestCtx) {
values := map[string]interface{}{
"Name": "go-sessions",
"Days": "1",
"Secret": "dsads£2132215£%%Ssdsa",
}
sess := sessions.StartFasthttp(reqCtx)
for k, v := range values {
sess.Set(k, v)
}
reqCtx.WriteString("Session saved, go to /get to view the results")
}
getHandler := func(reqCtx *fasthttp.RequestCtx) {
sess := sessions.StartFasthttp(reqCtx)
sessValues := sess.GetAll()
reqCtx.WriteString(fmt.Sprintf("%#v", sessValues))
}
clearHandler := func(reqCtx *fasthttp.RequestCtx) {
sess := sessions.StartFasthttp(reqCtx)
sess.Clear()
}
destroyHandler := func(reqCtx *fasthttp.RequestCtx) {
sessions.DestroyFasthttp(reqCtx)
}
fmt.Println("Open a browser tab and navigate to the localhost:8080/set")
fasthttp.ListenAndServe(":8080", func(reqCtx *fasthttp.RequestCtx) {
path := string(reqCtx.Path())
if path == "/set" {
setHandler(reqCtx)
} else if path == "/get" {
getHandler(reqCtx)
} else if path == "/clear" {
clearHandler(reqCtx)
} else if path == "/destroy" {
destroyHandler(reqCtx)
} else {
reqCtx.WriteString("Please navigate to /set or /get or /clear or /destroy")
}
})
}
FAQ
If you'd like to discuss this package, or ask questions about it, feel free to
Versioning
Current: v3.3.0
Read more about Semantic Versioning 2.0.0
People
The author of go-sessions is @kataras.
Contributing
If you are interested in contributing to the go-sessions project, please make a PR.
License
This project is licensed under the MIT License.
License can be found here.