1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 18:37:05 +00:00

Update sessions/sessiondb/badger to its latest version and SessionController is able to initialize itself if session manager is not provided by the caller-dev.

Changes:

Update the badger database using transactions, as supported from yesterday via: 06242925c2

Add a custom `OnActivate` via mvc/activator/activate_listener.go, this can be used to perform any custom actions when the app registers the supported Controllers. See mvc/session_controller.go for the excellent use case.

errors.Reporter.AddErr returns true if the error is added to the stack, otherwise false


Former-commit-id: c896a2d186a4315643f3c5fdb4325f7ee48a9e0a
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-10-06 01:19:10 +03:00
parent 6925ed9b33
commit 4fb78bbcd9
17 changed files with 232 additions and 70 deletions

View File

@@ -2,9 +2,14 @@ package mvc
import (
"github.com/kataras/iris/context"
"github.com/kataras/iris/mvc/activator"
"github.com/kataras/iris/sessions"
"github.com/kataras/golog"
)
var defaultManager = sessions.New(sessions.Config{})
// SessionController is a simple `Controller` implementation
// which requires a binded session manager in order to give
// direct access to the current client's session via its `Session` field.
@@ -15,21 +20,27 @@ type SessionController struct {
Session *sessions.Session
}
var managerMissing = "MVC SessionController: session manager field is nil, you have to bind it to a *sessions.Sessions"
// OnActivate called, once per application lifecycle NOT request,
// every single time the dev registers a specific SessionController-based controller.
// It makes sure that its "Manager" field is filled
// even if the caller didn't provide any sessions manager via the `app.Controller` function.
func (s *SessionController) OnActivate(p *activator.ActivatePayload) {
if p.EnsureBindValue(defaultManager) {
golog.Warnf(`MVC SessionController: couldn't find any "*sessions.Sessions" bindable value to fill the "Manager" field,
therefore this controller is using the default sessions manager instead.
Please refer to the documentation to learn how you can provide the session manager`)
}
}
// BeginRequest calls the Controller's BeginRequest
// and tries to initialize the current user's Session.
func (s *SessionController) BeginRequest(ctx context.Context) {
s.Controller.BeginRequest(ctx)
if s.Manager == nil {
ctx.Application().Logger().Errorf(managerMissing)
ctx.Application().Logger().Errorf(`MVC SessionController: sessions manager is nil, report this as a bug
because the SessionController should predict this on its activation state and use a default one automatically`)
return
}
s.Session = s.Manager.Start(ctx)
}
/* TODO:
Maybe add struct tags on `binder` for required binded values
in order to log error if some of the bindings are missing or leave that to the end-developers?
*/