iris/config/sessions.go
Makis Maropoulos 077984bd60 🌈 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.
2016-07-15 20:50:36 +03:00

85 lines
2.7 KiB
Go

package config
import (
"time"
"github.com/imdario/mergo"
)
var (
universe time.Time // 0001-01-01 00:00:00 +0000 UTC
// CookieExpireNever the default cookie's life for sessions, unlimited
CookieExpireNever = universe
)
const (
// DefaultCookieName the secret cookie's name for sessions
DefaultCookieName = "irissessionid"
// DefaultSessionGcDuration is the default Session Manager's GCDuration , which is 2 hours
DefaultSessionGcDuration = time.Duration(2) * time.Hour
)
type (
// Sessions the configuration for sessions
// 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 {
// 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
// Defaults to false
DecodeCookie bool
//Expires the date which the cookie must expires. Default infinitive/unlimited life
Expires time.Time
// GcDuration every how much duration(GcDuration) the memory should be clear for unused cookies (GcDuration)
// for example: time.Duration(2)*time.Hour. it will check every 2 hours if cookie hasn't be used for 2 hours,
// deletes it from memory until the user comes back, then the session continue to work as it was
//
// Default 2 hours
GcDuration time.Duration
// DisableSubdomainPersistence set it to true in order dissallow your iris subdomains to have access to the session cookie
// defaults to false
DisableSubdomainPersistence bool
}
)
// DefaultSessions the default configs for Sessions
func DefaultSessions() Sessions {
return Sessions{
Cookie: DefaultCookieName,
DecodeCookie: false,
Expires: CookieExpireNever,
GcDuration: DefaultSessionGcDuration,
DisableSubdomainPersistence: false,
}
}
// 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
}
// MergeSingle merges the default with the given config and returns the result
func (c Sessions) MergeSingle(cfg Sessions) (config Sessions) {
config = cfg
mergo.Merge(&config, c)
return
}