mirror of
https://github.com/kataras/iris.git
synced 2026-01-07 20:17:05 +00:00
🌈 sessions were re-written, update to 4.0.0-alpha.2, read HISTORY.md
**Sessions were re-written ** - Developers can use more than one 'session database', at the same time, to store the sessions - Easy to develop a custom session database (only two functions are required (Load & Update)), [learn more](https://github.com/iris-contrib/sessiondb/blob/master/redis/database.go) - Session databases are located [here](https://github.com/iris-contrib/sessiondb), contributions are welcome - The only frontend deleted 'thing' is the: **config.Sessions.Provider** - No need to register a database, the sessions works out-of-the-box - No frontend/API changes except the `context.Session().Set/Delete/Clear`, they doesn't return errors anymore, btw they (errors) were always nil :) - Examples (master branch) were updated. ```sh $ go get github.com/iris-contrib/sessiondb/$DATABASE ``` ```go db := $DATABASE.New(configurationHere{}) iris.UseSessionDB(db) ``` > Note: Book is not updated yet, examples are up-to-date as always.
This commit is contained in:
@@ -17,50 +17,18 @@ const (
|
||||
DefaultCookieName = "irissessionid"
|
||||
// DefaultSessionGcDuration is the default Session Manager's GCDuration , which is 2 hours
|
||||
DefaultSessionGcDuration = time.Duration(2) * time.Hour
|
||||
// DefaultRedisNetwork the redis network option, "tcp"
|
||||
DefaultRedisNetwork = "tcp"
|
||||
// DefaultRedisAddr the redis address option, "127.0.0.1:6379"
|
||||
DefaultRedisAddr = "127.0.0.1:6379"
|
||||
// DefaultRedisIdleTimeout the redis idle timeout option, time.Duration(5) * time.Minute
|
||||
DefaultRedisIdleTimeout = time.Duration(5) * time.Minute
|
||||
// DefaultRedisMaxAgeSeconds the redis storage last parameter (SETEX), 31556926.0 (1 year)
|
||||
DefaultRedisMaxAgeSeconds = 31556926.0 //1 year
|
||||
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
// Redis the redis configuration used inside sessions
|
||||
Redis struct {
|
||||
// Network "tcp"
|
||||
Network string
|
||||
// Addr "127.0.0.1:6379"
|
||||
Addr string
|
||||
// Password string .If no password then no 'AUTH'. Default ""
|
||||
Password string
|
||||
// If Database is empty "" then no 'SELECT'. Default ""
|
||||
Database string
|
||||
// MaxIdle 0 no limit
|
||||
MaxIdle int
|
||||
// MaxActive 0 no limit
|
||||
MaxActive int
|
||||
// IdleTimeout time.Duration(5) * time.Minute
|
||||
IdleTimeout time.Duration
|
||||
// Prefix "myprefix-for-this-website". Default ""
|
||||
Prefix string
|
||||
// MaxAgeSeconds how much long the redis should keep the session in seconds. Default 31556926.0 (1 year)
|
||||
MaxAgeSeconds int
|
||||
}
|
||||
|
||||
// Sessions the configuration for sessions
|
||||
// has 4 fields
|
||||
// first is the providerName (string) ["memory","redis"]
|
||||
// second is the cookieName, the session's name (string) ["mysessionsecretcookieid"]
|
||||
// has 5 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 gcDuration (time.Duration) when this time passes it removes the unused sessions from the memory until the user come back
|
||||
// fifth is the DisableSubdomainPersistence which you can set it to true in order dissallow your iris subdomains to have access to the session cook
|
||||
Sessions struct {
|
||||
// Provider string, usage iris.Config().Provider = "memory" or "redis". If you wan to customize redis then import the package, and change it's config
|
||||
Provider string
|
||||
// Cookie string, the session's client cookie name, for example: "irissessionid"
|
||||
Cookie string
|
||||
// DecodeCookie set it to true to decode the cookie key with base64 URLEncoding
|
||||
@@ -75,7 +43,7 @@ type (
|
||||
// Default 2 hours
|
||||
GcDuration time.Duration
|
||||
|
||||
// DisableSubdomainPersistence set it to dissallow your iris subdomains to have access to the session cookie
|
||||
// DisableSubdomainPersistence set it to true in order dissallow your iris subdomains to have access to the session cookie
|
||||
// defaults to false
|
||||
DisableSubdomainPersistence bool
|
||||
}
|
||||
@@ -84,7 +52,6 @@ type (
|
||||
// DefaultSessions the default configs for Sessions
|
||||
func DefaultSessions() Sessions {
|
||||
return Sessions{
|
||||
Provider: "memory", // the default provider is "memory", if you set it to "" means that sessions are disabled.
|
||||
Cookie: DefaultCookieName,
|
||||
DecodeCookie: false,
|
||||
Expires: CookieExpireNever,
|
||||
@@ -115,41 +82,3 @@ func (c Sessions) MergeSingle(cfg Sessions) (config Sessions) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DefaultRedis returns the default configuration for Redis service
|
||||
func DefaultRedis() Redis {
|
||||
return Redis{
|
||||
Network: DefaultRedisNetwork,
|
||||
Addr: DefaultRedisAddr,
|
||||
Password: "",
|
||||
Database: "",
|
||||
MaxIdle: 0,
|
||||
MaxActive: 0,
|
||||
IdleTimeout: DefaultRedisIdleTimeout,
|
||||
Prefix: "",
|
||||
MaxAgeSeconds: DefaultRedisMaxAgeSeconds,
|
||||
}
|
||||
}
|
||||
|
||||
// Merge merges the default with the given config and returns the result
|
||||
func (c Redis) Merge(cfg []Redis) (config Redis) {
|
||||
|
||||
if len(cfg) > 0 {
|
||||
config = cfg[0]
|
||||
mergo.Merge(&config, c)
|
||||
} else {
|
||||
_default := c
|
||||
config = _default
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MergeSingle merges the default with the given config and returns the result
|
||||
func (c Redis) MergeSingle(cfg Redis) (config Redis) {
|
||||
|
||||
config = cfg
|
||||
mergo.Merge(&config, c)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user