diff options
-rw-r--r-- | sessions.go | 26 | ||||
-rw-r--r-- | store.go | 21 |
2 files changed, 24 insertions, 23 deletions
diff --git a/sessions.go b/sessions.go index 39930cf..38d486a 100644 --- a/sessions.go +++ b/sessions.go @@ -11,6 +11,7 @@ import ( "time" "github.com/webx-top/echo" + "github.com/webx-top/echo/engine" ) // Default flashes key. @@ -91,7 +92,7 @@ func (s *Session) AddFlash(value interface{}, vars ...string) { // store.Save(request, response, session). You should call Save before writing to // the response or returning from the handler. func (s *Session) Save(ctx echo.Context) error { - return s.store.Save(ctx, s) + return s.store.Save(ctx.Request(), ctx.Response(), s) } // Name returns the name used to register the session. @@ -116,27 +117,26 @@ type sessionInfo struct { type contextKey int // registryKey is the key used to store the registry in the context. -//const registryKey contextKey = 0 - -const registryKey = `webx:sessionsRegistry` +const registryKey contextKey = 0 // GetRegistry returns a registry instance for the current request. -func GetRegistry(context echo.Context) *Registry { - registry := context.Get(registryKey) +func GetRegistry(ctx echo.Context) *Registry { + r := ctx.Request() + registry := engine.Get(r, registryKey) if registry != nil { return registry.(*Registry) } newRegistry := &Registry{ - context: context, + request: r, sessions: make(map[string]sessionInfo), } - context.Set(registryKey, newRegistry) + engine.Set(r, registryKey, newRegistry) return newRegistry } // Registry stores sessions used during a request. type Registry struct { - context echo.Context + request engine.Request sessions map[string]sessionInfo } @@ -147,7 +147,7 @@ func (s *Registry) Get(store Store, name string) (session *Session, err error) { if info, ok := s.sessions[name]; ok { session, err = info.s, info.e } else { - session, err = store.New(s.context, name) + session, err = store.New(s.request, name) session.name = name s.sessions[name] = sessionInfo{s: session, e: err} } @@ -156,14 +156,14 @@ func (s *Registry) Get(store Store, name string) (session *Session, err error) { } // Save saves all sessions registered for the current request. -func (s *Registry) Save() error { +func (s *Registry) Save(w engine.Response) error { var errMulti MultiError for name, info := range s.sessions { session := info.s if session.store == nil { errMulti = append(errMulti, fmt.Errorf( "sessions: missing store for session %q", name)) - } else if err := session.store.Save(s.context, session); err != nil { + } else if err := session.store.Save(s.request, w, session); err != nil { errMulti = append(errMulti, fmt.Errorf( "sessions: error saving session %q -- %v", name, err)) } @@ -182,7 +182,7 @@ func init() { // Save saves all sessions used during the current request. func Save(ctx echo.Context) error { - return GetRegistry(ctx).Save() + return GetRegistry(ctx).Save(ctx.Response()) } // NewCookie returns an http.Cookie with the options set. It also sets @@ -14,6 +14,7 @@ import ( "github.com/gorilla/securecookie" "github.com/webx-top/echo" + "github.com/webx-top/echo/engine" ) // Store is an interface for custom session stores. @@ -27,10 +28,10 @@ type Store interface { // // Note that New should never return a nil session, even in the case of // an error if using the Registry infrastructure to cache the session. - New(ctx echo.Context, name string) (*Session, error) + New(r engine.Request, name string) (*Session, error) // Save should persist session to the underlying store implementation. - Save(ctx echo.Context, s *Session) error + Save(r engine.Request, w engine.Response, s *Session) error } // CookieStore ---------------------------------------------------------------- @@ -85,13 +86,13 @@ func (s *CookieStore) Get(ctx echo.Context, name string) (*Session, error) { // The difference between New() and Get() is that calling New() twice will // decode the session data twice, while Get() registers and reuses the same // decoded session after the first call. -func (s *CookieStore) New(ctx echo.Context, name string) (*Session, error) { +func (s *CookieStore) New(r engine.Request, name string) (*Session, error) { session := NewSession(s, name) opts := *s.Options session.Options = &opts session.IsNew = true var err error - if v := ctx.Request().Cookie(name); v != `` { + if v := r.Cookie(name); v != `` { err = securecookie.DecodeMulti(name, v, &session.Values, s.Codecs...) if err == nil { @@ -102,14 +103,14 @@ func (s *CookieStore) New(ctx echo.Context, name string) (*Session, error) { } // Save adds a single session to the response. -func (s *CookieStore) Save(ctx echo.Context, +func (s *CookieStore) Save(r engine.Request, w engine.Response, session *Session) error { encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, s.Codecs...) if err != nil { return err } - ctx.Response().SetCookie(NewCookie(session.Name(), encoded, session.Options)) + w.SetCookie(NewCookie(session.Name(), encoded, session.Options)) return nil } @@ -186,13 +187,13 @@ func (s *FilesystemStore) Get(ctx echo.Context, name string) (*Session, error) { // New returns a session for the given name without adding it to the registry. // // See CookieStore.New(). -func (s *FilesystemStore) New(ctx echo.Context, name string) (*Session, error) { +func (s *FilesystemStore) New(r engine.Request, name string) (*Session, error) { session := NewSession(s, name) opts := *s.Options session.Options = &opts session.IsNew = true var err error - if v := ctx.Request().Cookie(name); v != `` { + if v := r.Cookie(name); v != `` { err = securecookie.DecodeMulti(name, v, &session.ID, s.Codecs...) if err == nil { err = s.load(session) @@ -205,7 +206,7 @@ func (s *FilesystemStore) New(ctx echo.Context, name string) (*Session, error) { } // Save adds a single session to the response. -func (s *FilesystemStore) Save(ctx echo.Context, +func (s *FilesystemStore) Save(r engine.Request, w engine.Response, session *Session) error { if session.ID == "" { // Because the ID is used in the filename, encode it to @@ -222,7 +223,7 @@ func (s *FilesystemStore) Save(ctx echo.Context, if err != nil { return err } - ctx.Response().SetCookie(NewCookie(session.Name(), encoded, session.Options)) + w.SetCookie(NewCookie(session.Name(), encoded, session.Options)) return nil } |