1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +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:
Makis Maropoulos
2016-07-15 20:50:36 +03:00
parent af4df18ec4
commit 077984bd60
20 changed files with 733 additions and 1582 deletions

View File

@@ -25,7 +25,6 @@ import (
"github.com/iris-contrib/formBinder"
"github.com/kataras/iris/config"
"github.com/kataras/iris/context"
"github.com/kataras/iris/sessions/store"
"github.com/kataras/iris/utils"
"github.com/klauspost/compress/gzip"
"github.com/valyala/fasthttp"
@@ -76,10 +75,6 @@ var (
)
type (
// RenderOptions is a helper type for the optional runtime options can be passed by user when Render
// an example of this is the "layout" or "gzip" option
// same as Map but more specific name
RenderOptions map[string]interface{}
// Map is just a conversion for a map[string]interface{}
// should not be used inside Render when PongoEngine is used.
@@ -91,8 +86,8 @@ type (
Params PathParameters
framework *Framework
//keep track all registed middleware (handlers)
middleware Middleware
sessionStore store.IStore
middleware Middleware
session *session
// pos is the position number of the Context, look .Next to understand
pos uint8
}
@@ -110,7 +105,7 @@ func (ctx *Context) GetRequestCtx() *fasthttp.RequestCtx {
// I use it for zero rellocation memory
func (ctx *Context) Reset(reqCtx *fasthttp.RequestCtx) {
ctx.Params = ctx.Params[0:0]
ctx.sessionStore = nil
ctx.session = nil
ctx.middleware = nil
ctx.RequestCtx = reqCtx
}
@@ -853,24 +848,32 @@ func (ctx *Context) SetFlash(key string, value string) {
fasthttp.ReleaseCookie(c)
}
// Session returns the current session store, returns nil if provider is ""
func (ctx *Context) Session() store.IStore {
if ctx.framework.sessions == nil || ctx.framework.Config.Sessions.Provider == "" { //the second check can be changed on runtime, users are able to turn off the sessions by setting provider to ""
// Session returns the current session
func (ctx *Context) 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()
} {
if ctx.framework.sessions == nil { // this should never return nil but FOR ANY CASE, on future changes.
return nil
}
if ctx.sessionStore == nil {
ctx.sessionStore = ctx.framework.sessions.Start(ctx)
if ctx.session == nil {
ctx.session = ctx.framework.sessions.start(ctx)
}
return ctx.sessionStore
return ctx.session
}
// SessionDestroy destroys the whole session, calls the provider's destroy and remove the cookie
func (ctx *Context) SessionDestroy() {
if ctx.framework.sessions != nil {
if store := ctx.Session(); store != nil {
ctx.framework.sessions.Destroy(ctx)
}
if sess := ctx.Session(); sess != nil {
ctx.framework.sessions.destroy(ctx)
}
}