From 352b297977a3055f0f7cb4335bf97ee1abcb8040 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Wed, 20 Jul 2016 20:23:54 +0300 Subject: [PATCH] Fix https://github.com/iris-contrib/sessiondb/issues/1 --- iris.go | 12 ++++++------ sessions.go | 9 +++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/iris.go b/iris.go index 5714b3b7..e1a1d30f 100644 --- a/iris.go +++ b/iris.go @@ -219,6 +219,11 @@ func New(cfg ...config.Iris) *Framework { }, engines: make([]*templateEngineWrapper, 0), } + // set the sessions + if s.Config.Sessions.Cookie != "" { + //set the session manager + s.sessions = newSessionsManager(&s.Config.Sessions) + } // set the websocket server s.Websocket = NewWebsocketServer(s.Config.Websocket) // set the servemux, which will provide us the public API also, with its context pool @@ -234,10 +239,6 @@ func New(cfg ...config.Iris) *Framework { } func (s *Framework) initialize() { - if s.Config.Sessions.Cookie != "" { - //set the session manager - s.sessions = newSessionsManager(s.Config.Sessions) - } // prepare the response engines, if no response engines setted for the default content-types // then add them @@ -275,7 +276,6 @@ func (s *Framework) initialize() { s.Logger.Panic(err) // panic on templates loading before listening if we have an error. } } - // listen to websocket connections RegisterWebsocketServer(s, s.Websocket, s.Logger) @@ -528,7 +528,7 @@ func UseSessionDB(db SessionDatabase) { // // Note: Don't worry if no session database is registered, your context.Session will continue to work. func (s *Framework) UseSessionDB(db SessionDatabase) { - s.sessions.provider.registerDatabase(db) + s.sessions.registerDatabase(db) } // UseResponse accepts a ResponseEngine and the key or content type on which the developer wants to register this response engine diff --git a/sessions.go b/sessions.go index 8304babd..3fdeeeea 100644 --- a/sessions.go +++ b/sessions.go @@ -267,13 +267,13 @@ type ( // sessionsManager implements the ISessionsManager interface // contains the cookie's name, the provider and a duration for GC and cookie life expire sessionsManager struct { - config config.Sessions + config *config.Sessions provider *sessionProvider } ) // newSessionsManager creates & returns a new SessionsManager and start its GC -func newSessionsManager(c config.Sessions) *sessionsManager { +func newSessionsManager(c *config.Sessions) *sessionsManager { if c.DecodeCookie { c.Cookie = base64.URLEncoding.EncodeToString([]byte(c.Cookie)) // change the cookie's name/key to a more safe(?) // get the real value for your tests by: @@ -285,6 +285,11 @@ func newSessionsManager(c config.Sessions) *sessionsManager { return manager } +func (m *sessionsManager) registerDatabase(db SessionDatabase) { + m.provider.expires = m.config.Expires // updae the expires confiuration field for any case + m.provider.registerDatabase(db) +} + func (m *sessionsManager) generateSessionID() string { return base64.URLEncoding.EncodeToString(utils.Random(32)) }