Fast, unique &
cross-framework http sessions for Go.
Easy to
learn, while providing robust set of features.
Ideally suited for both experienced and novice Developers.
Quick view
import "github.com/fasthttp-contrib/sessions"
sess := sessions.StartFasthttp(*fasthttp.RequestCtx)
sess.ID() string
sess.Get(string) interface{}
sess.GetString(key string) string
sess.GetInt(key string) int
sess.GetAll() map[string]interface{}
sess.VisitAll(cb func(k string, v interface{}))
sess.Set(string, interface{})
sess.Delete(string)
sess.Clear()
Installation
The only requirement is the Go Programming Language, at least v1.7.
$ go get -u github.com/fasthttp-contrib/sessions
Features
- Focus on simplicity and performance, it's the fastest sessions provider in Go world.
- Cleans the temp memory when a session is idle, and re-allocates it to the temp memory when it's necessary.
- The most used sessions are optimized to be in the front of the memory's list.
- Supports any type of external database.
- Works with both net/http and valyala/fasthttp.
Docs
Take a look at the ./examples.
OUTLINE
StartFasthttp(*fasthttp.RequestCtx) Session
DestroyFasthttp(*fasthttp.RequestCtx)
Start(http.ResponseWriter, *http.Request) Session
Destroy(http.ResponseWriter, *http.Request)
UseDatabase(Database)
UpdateConfig(Config)
Usage FASTHTTP
StartFasthttp
returns again Session
, Session outline
type Session interface {
ID() string
Get(string) interface{}
GetString(key string) string
GetInt(key string) int
GetAll() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
Delete(string)
Clear()
}
package main
import (
"fmt"
"github.com/kataras/go-sessions"
"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")
}
})
}
Usage NET/HTTP
Start
returns a Session
, Session outline
type Session interface {
ID() string
Get(string) interface{}
GetString(key string) string
GetInt(key string) int
GetAll() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
Delete(string)
Clear()
}
package main
import (
"fmt"
"github.com/kataras/go-sessions"
"net/http"
)
func main() {
setHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
values := map[string]interface{}{
"Name": "go-sessions",
"Days": "1",
"Secret": "dsads£2132215£%%Ssdsa",
}
sess := sessions.Start(res, req)
for k, v := range values {
sess.Set(k, v)
}
res.Write([]byte("Session saved, go to /get to view the results"))
})
http.Handle("/set/", setHandler)
getHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
sess := sessions.Start(res, req)
sessValues := sess.GetAll()
res.Write([]byte(fmt.Sprintf("%#v", sessValues)))
})
http.Handle("/get/", getHandler)
clearHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
sess := sessions.Start(res, req)
sess.Clear()
})
http.Handle("/clear/", clearHandler)
destroyHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
sessions.Destroy(res, req)
})
http.Handle("/destroy/", destroyHandler)
fmt.Println("Open a browser tab and navigate to the localhost:8080/set/")
http.ListenAndServe(":8080", nil)
}
FAQ
If you'd like to discuss this package, or ask questions about it, feel free to
Versioning
Current: v0.0.3
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.