You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fasthttpsession is a session manager for Go. It only supports fasthttp, currently support providers:
file
memcache
memory
mysql
postgres
redis
sqlite3
Features
Focus on the design of the code architecture and expansion.
Provide full session storage.
Convenient switching of session storage.
Customizable data serialization.
Implement concurrent map(ccmap.go) to improve performance.
Install
The only requirement is the Go Programming Language, at least v1.7
$ go get -u github.com/phachon/fasthttpsession
$ go get ./...
Used
Quick Start
// fasthttpsession use memory providerimport (
"github.com/phachon/fasthttpsession""github.com/phachon/fasthttpsession/memory""github.com/valyala/fasthttp""log""os"
)
// default configvarsession=fasthttpsession.NewSession(fasthttpsession.NewDefaultConfig())
funcmain() {
// you must set up provider before useerr:=session.SetProvider("memory", &memory.Config{})
iferr!=nil {
log.Println(err.Error())
os.Exit(1)
}
addr:=":8086"log.Println("fasthttpsession example server listen: "+addr)
// fasthttp start listen serveerr=fasthttp.ListenAndServe(addr, requestHandle)
iferr!=nil {
log.Println("listen server error :"+err.Error())
}
}
// request handlerfuncrequestHandle(ctx*fasthttp.RequestCtx) {
// start sessionsessionStore, err:=session.Start(ctx)
iferr!=nil {
ctx.SetBodyString(err.Error())
return
}
// must defer sessionStore.save(ctx)defersessionStore.Save(ctx)
sessionStore.Set("name", "fasthttpsession")
ctx.SetBodyString(fmt.Sprintf("fasthttpsession setted key name= %s ok", sessionStore.Get("name").(string)))
}
Custom configuration
If you don't want to use the default configuration, please use the following struct custom.
typeConfigstruct {
// cookie nameCookieNamestring// cookie domainDomainstring// If you want to delete the cookie when the browser closes, set it to -1.//// 0 means no expire, (24 years)// -1 means when browser closes// >0 is the time.Duration which the session cookies should expire.Expires time.Duration// gc life time(s)GCLifetimeint64// session life time(s)SessionLifetimeint64// set whether to pass this bar cookie only through HTTPSSecurebool// sessionId is in url querySessionIdInURLQuerybool// sessionName in url querySessionNameInUrlQuerystring// sessionId is in http headerSessionIdInHttpHeaderbool// sessionName in http headerSessionNameInHttpHeaderstring// SessionIdGeneratorFunc should returns a random session id.SessionIdGeneratorFuncfunc() string// Encode the cookie value if not nil.EncodeFuncfunc(cookieValuestring) (string, error)
// Decode the cookie value if not nil.DecodeFuncfunc(cookieValuestring) (string, error)
}
Different session provider config, please look at the Config.go the provider name directory.