iris/config/sessions.go

69 lines
2.1 KiB
Go
Raw Normal View History

2016-05-30 16:08:09 +02:00
package config
import (
"github.com/imdario/mergo"
"github.com/kataras/go-sessions/fasthttp"
"time"
2016-05-30 16:08:09 +02:00
)
var (
universe time.Time // 0001-01-01 00:00:00 +0000 UTC
// CookieExpireNever the default cookie's life for sessions, unlimited (23 years)
CookieExpireNever = time.Now().AddDate(23, 0, 0)
2016-05-30 16:08:09 +02:00
)
const (
// DefaultCookieName the secret cookie's name for sessions
2016-06-03 04:11:50 +02:00
DefaultCookieName = "irissessionid"
// DefaultSessionGcDuration is the default Session Manager's GCDuration , which is 2 hours
2016-05-30 16:08:09 +02:00
DefaultSessionGcDuration = time.Duration(2) * time.Hour
// DefaultCookieLength is the default Session Manager's CookieLength, which is 32
DefaultCookieLength = 32
2016-05-30 16:08:09 +02:00
)
// Sessions the configuration for sessions
// has 6 fields
// first is the cookieName, the session's name (string) ["mysessionsecretcookieid"]
// second enable if you want to decode the cookie's key also
// third is the time which the client's cookie expires
// forth is the cookie length (sessionid) int, defaults to 32, do not change if you don't have any reason to do
// fifth is the gcDuration (time.Duration) when this time passes it removes the unused sessions from the memory until the user come back
// sixth is the DisableSubdomainPersistence which you can set it to true in order dissallow your q subdomains to have access to the session cook
//
type Sessions sessions.Config
2016-05-30 16:08:09 +02:00
// DefaultSessions the default configs for Sessions
func DefaultSessions() Sessions {
return Sessions{
Cookie: DefaultCookieName,
CookieLength: DefaultCookieLength,
DecodeCookie: false,
Expires: 0,
GcDuration: DefaultSessionGcDuration,
DisableSubdomainPersistence: false,
2016-05-30 16:08:09 +02:00
}
}
// Merge merges the default with the given config and returns the result
func (c Sessions) Merge(cfg []Sessions) (config Sessions) {
if len(cfg) > 0 {
config = cfg[0]
mergo.Merge(&config, c)
} else {
_default := c
config = _default
}
return
}
2016-06-03 04:11:50 +02:00
// MergeSingle merges the default with the given config and returns the result
2016-05-30 16:08:09 +02:00
func (c Sessions) MergeSingle(cfg Sessions) (config Sessions) {
config = cfg
mergo.Merge(&config, c)
return
}