diff options
author | lyric <tiannianshou@gmail.com> | 2018-10-19 06:32:16 +0300 |
---|---|---|
committer | lyric <tiannianshou@gmail.com> | 2018-10-19 06:32:16 +0300 |
commit | b37294e622863ea95e6005d5899f83baa570bc15 (patch) | |
tree | 96920ffdb737f0ea187a6bf6412177be949c0d20 | |
parent | 7bb00b60ac02f5c0a19bf95d2b093a5d5cf45666 (diff) |
-rw-r--r-- | session.go | 36 |
1 files changed, 17 insertions, 19 deletions
@@ -1,9 +1,6 @@ package echosession import ( - "context" - "sync" - "github.com/go-session/session" "github.com/labstack/echo" ) @@ -19,29 +16,23 @@ type ( Skipper Skipper // StoreKey keys stored in the context StoreKey string + // ManageKey keys stored in the context + ManageKey string } ) var ( // DefaultConfig is the default Recover middleware config. DefaultConfig = Config{ - Skipper: func(_ echo.Context) bool { return false }, - StoreKey: "github.com/go-session/echo-session", + Skipper: func(_ echo.Context) bool { return false }, + StoreKey: "github.com/go-session/echo-session/store", + ManageKey: "github.com/go-session/echo-session/manage", } - once sync.Once - internalManager *session.Manager - storeKey string + storeKey string + manageKey string ) -// get a session manager -func manager(opt ...session.Option) *session.Manager { - once.Do(func() { - internalManager = session.NewManager(opt...) - }) - return internalManager -} - // New create a session middleware func New(opt ...session.Option) echo.MiddlewareFunc { return NewWithConfig(DefaultConfig, opt...) @@ -53,18 +44,25 @@ func NewWithConfig(config Config, opt ...session.Option) echo.MiddlewareFunc { config.Skipper = DefaultConfig.Skipper } + manageKey = config.ManageKey + if manageKey == "" { + manageKey = DefaultConfig.ManageKey + } + storeKey = config.StoreKey if storeKey == "" { storeKey = DefaultConfig.StoreKey } + manage := session.NewManager(opt...) return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { if config.Skipper(c) { return next(c) } - store, err := manager(opt...).Start(context.Background(), c.Response(), c.Request()) + c.Set(manageKey, manage) + store, err := manage.Start(nil, c.Response(), c.Request()) if err != nil { return err } @@ -81,10 +79,10 @@ func FromContext(ctx echo.Context) session.Store { // Destroy a session func Destroy(ctx echo.Context) error { - return manager().Destroy(context.Background(), ctx.Response(), ctx.Request()) + return ctx.Get(manageKey).(*session.Manager).Destroy(nil, ctx.Response(), ctx.Request()) } // Refresh a session and return to session storage func Refresh(ctx echo.Context) (session.Store, error) { - return manager().Refresh(context.Background(), ctx.Response(), ctx.Request()) + return ctx.Get(manageKey).(*session.Manager).Refresh(nil, ctx.Response(), ctx.Request()) } |