fix -- for echo framework

This commit is contained in:
swh 2016-02-18 16:44:42 +08:00
parent 715060ac0b
commit 84f333210a
2 changed files with 31 additions and 30 deletions

View file

@ -10,7 +10,7 @@ import (
"net/http"
"time"
"github.com/gorilla/context"
"github.com/webx-top/echo"
)
// Default flashes key.
@ -90,8 +90,8 @@ func (s *Session) AddFlash(value interface{}, vars ...string) {
// Save is a convenience method to save this session. It is the same as calling
// store.Save(request, response, session). You should call Save before writing to
// the response or returning from the handler.
func (s *Session) Save(r *http.Request, w http.ResponseWriter) error {
return s.store.Save(r, w, s)
func (s *Session) Save(ctx echo.Context) error {
return s.store.Save(ctx, s)
}
// Name returns the name used to register the session.
@ -119,22 +119,22 @@ type contextKey int
const registryKey contextKey = 0
// GetRegistry returns a registry instance for the current request.
func GetRegistry(r *http.Request) *Registry {
registry := context.Get(r, registryKey)
func GetRegistry(context echo.Context) *Registry {
registry := context.Get(registryKey)
if registry != nil {
return registry.(*Registry)
}
newRegistry := &Registry{
request: r,
context: context,
sessions: make(map[string]sessionInfo),
}
context.Set(r, registryKey, newRegistry)
context.Set(registryKey, newRegistry)
return newRegistry
}
// Registry stores sessions used during a request.
type Registry struct {
request *http.Request
context echo.Context
sessions map[string]sessionInfo
}
@ -145,7 +145,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.request, name)
session, err = store.New(s.context, name)
session.name = name
s.sessions[name] = sessionInfo{s: session, e: err}
}
@ -154,14 +154,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(w http.ResponseWriter) error {
func (s *Registry) Save() 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.request, w, session); err != nil {
} else if err := session.store.Save(s.context, session); err != nil {
errMulti = append(errMulti, fmt.Errorf(
"sessions: error saving session %q -- %v", name, err))
}
@ -179,8 +179,8 @@ func init() {
}
// Save saves all sessions used during the current request.
func Save(r *http.Request, w http.ResponseWriter) error {
return GetRegistry(r).Save(w)
func Save(ctx echo.Context) error {
return GetRegistry(ctx).Save()
}
// NewCookie returns an http.Cookie with the options set. It also sets

View file

@ -14,6 +14,7 @@ import (
"sync"
"github.com/gorilla/securecookie"
"github.com/webx-top/echo"
)
// Store is an interface for custom session stores.
@ -21,16 +22,16 @@ import (
// See CookieStore and FilesystemStore for examples.
type Store interface {
// Get should return a cached session.
Get(r *http.Request, name string) (*Session, error)
Get(ctx echo.Context, name string) (*Session, error)
// New should create and return a new session.
//
// 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(r *http.Request, name string) (*Session, error)
New(ctx echo.Context, name string) (*Session, error)
// Save should persist session to the underlying store implementation.
Save(r *http.Request, w http.ResponseWriter, s *Session) error
Save(ctx echo.Context, s *Session) error
}
// CookieStore ----------------------------------------------------------------
@ -76,8 +77,8 @@ type CookieStore struct {
//
// It returns a new session and an error if the session exists but could
// not be decoded.
func (s *CookieStore) Get(r *http.Request, name string) (*Session, error) {
return GetRegistry(r).Get(s, name)
func (s *CookieStore) Get(ctx echo.Context, name string) (*Session, error) {
return GetRegistry(ctx).Get(s, name)
}
// New returns a session for the given name without adding it to the registry.
@ -85,14 +86,14 @@ func (s *CookieStore) Get(r *http.Request, 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(r *http.Request, name string) (*Session, error) {
func (s *CookieStore) New(ctx echo.Context, name string) (*Session, error) {
session := NewSession(s, name)
opts := *s.Options
session.Options = &opts
session.IsNew = true
var err error
if c, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, c.Value, &session.Values,
if v := ctx.Request().Cookie(name); v != `` {
err = securecookie.DecodeMulti(name, v, &session.Values,
s.Codecs...)
if err == nil {
session.IsNew = false
@ -102,14 +103,14 @@ func (s *CookieStore) New(r *http.Request, name string) (*Session, error) {
}
// Save adds a single session to the response.
func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter,
func (s *CookieStore) Save(ctx echo.Context,
session *Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
s.Codecs...)
if err != nil {
return err
}
http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options))
ctx.Response().SetCookie(NewCookie(session.Name(), encoded, session.Options))
return nil
}
@ -179,21 +180,21 @@ func (s *FilesystemStore) MaxLength(l int) {
// Get returns a session for the given name after adding it to the registry.
//
// See CookieStore.Get().
func (s *FilesystemStore) Get(r *http.Request, name string) (*Session, error) {
return GetRegistry(r).Get(s, name)
func (s *FilesystemStore) Get(ctx echo.Context, name string) (*Session, error) {
return GetRegistry(ctx).Get(s, name)
}
// New returns a session for the given name without adding it to the registry.
//
// See CookieStore.New().
func (s *FilesystemStore) New(r *http.Request, name string) (*Session, error) {
func (s *FilesystemStore) New(ctx echo.Context, name string) (*Session, error) {
session := NewSession(s, name)
opts := *s.Options
session.Options = &opts
session.IsNew = true
var err error
if c, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, c.Value, &session.ID, s.Codecs...)
if v := ctx.Request().Cookie(name); v != `` {
err = securecookie.DecodeMulti(name, v, &session.ID, s.Codecs...)
if err == nil {
err = s.load(session)
if err == nil {
@ -205,7 +206,7 @@ func (s *FilesystemStore) New(r *http.Request, name string) (*Session, error) {
}
// Save adds a single session to the response.
func (s *FilesystemStore) Save(r *http.Request, w http.ResponseWriter,
func (s *FilesystemStore) Save(ctx echo.Context,
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(r *http.Request, w http.ResponseWriter,
if err != nil {
return err
}
http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options))
ctx.Response().SetCookie(NewCookie(session.Name(), encoded, session.Options))
return nil
}