adapt CruSession
This commit is contained in:
parent
c4b585741a
commit
a98edc03cd
@ -3,27 +3,12 @@ package session
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"github.com/beego/beego/v2/core/logs"
|
"github.com/beego/beego/v2/core/logs"
|
||||||
"github.com/beego/beego/v2/server/web"
|
"github.com/beego/beego/v2/server/web"
|
||||||
webContext "github.com/beego/beego/v2/server/web/context"
|
webContext "github.com/beego/beego/v2/server/web/context"
|
||||||
"github.com/beego/beego/v2/server/web/session"
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
sessionFormatSign = uuid.New().ID()
|
|
||||||
defaultStorageKey = uuid.New().String()
|
|
||||||
)
|
|
||||||
|
|
||||||
func sessionStoreKey(key string) string {
|
|
||||||
return fmt.Sprintf(
|
|
||||||
`sess_%d:%s`,
|
|
||||||
sessionFormatSign,
|
|
||||||
key,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
//Session maintain session for web service
|
//Session maintain session for web service
|
||||||
//Session new a session storage and store it into webContext.Context
|
//Session new a session storage and store it into webContext.Context
|
||||||
//
|
//
|
||||||
@ -32,20 +17,23 @@ func sessionStoreKey(key string) string {
|
|||||||
//storeKey: set the storage key in ctx.Input
|
//storeKey: set the storage key in ctx.Input
|
||||||
//
|
//
|
||||||
//if you want to get session storage, just see GetStore
|
//if you want to get session storage, just see GetStore
|
||||||
func Session(providerType session.ProviderType, storeKey string, options ...session.ManagerConfigOpt) web.FilterChain {
|
func Session(providerType session.ProviderType, options ...session.ManagerConfigOpt) web.FilterChain {
|
||||||
sessionConfig := session.NewManagerConfig(options...)
|
sessionConfig := session.NewManagerConfig(options...)
|
||||||
sessionManager, _ := session.NewManager(string(providerType), sessionConfig)
|
sessionManager, _ := session.NewManager(string(providerType), sessionConfig)
|
||||||
go sessionManager.GC()
|
go sessionManager.GC()
|
||||||
|
|
||||||
return func(next web.FilterFunc) web.FilterFunc {
|
return func(next web.FilterFunc) web.FilterFunc {
|
||||||
return func(ctx *webContext.Context) {
|
return func(ctx *webContext.Context) {
|
||||||
|
if ctx.Input.CruSession != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if sess, err := sessionManager.SessionStart(ctx.ResponseWriter, ctx.Request); err != nil {
|
if sess, err := sessionManager.SessionStart(ctx.ResponseWriter, ctx.Request); err != nil {
|
||||||
logs.Warning(`init session error:%s`, err.Error())
|
logs.Warning(`init session error:%s`, err.Error())
|
||||||
} else {
|
} else {
|
||||||
//release session at the end of request
|
//release session at the end of request
|
||||||
defer sess.SessionRelease(context.Background(), ctx.ResponseWriter)
|
defer sess.SessionRelease(context.Background(), ctx.ResponseWriter)
|
||||||
ctx.Input.SetData(sessionStoreKey(storeKey), sess)
|
ctx.Input.CruSession = sess
|
||||||
}
|
}
|
||||||
|
|
||||||
next(ctx)
|
next(ctx)
|
||||||
@ -55,13 +43,13 @@ func Session(providerType session.ProviderType, storeKey string, options ...sess
|
|||||||
}
|
}
|
||||||
|
|
||||||
//GetStore get session storage in beego web context
|
//GetStore get session storage in beego web context
|
||||||
func GetStore(ctx *webContext.Context, storeKey string) (store session.Store, err error) {
|
func GetStore(ctx *webContext.Context) (store session.Store, err error) {
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
err = errors.New(`ctx is nil`)
|
err = errors.New(`ctx is nil`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if s, ok := ctx.Input.GetData(sessionStoreKey(storeKey)).(session.Store); ok {
|
if s := ctx.Input.CruSession; s != nil {
|
||||||
store = s
|
store = s
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
@ -69,13 +57,3 @@ func GetStore(ctx *webContext.Context, storeKey string) (store session.Store, er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//DefaultSession call Session with default storage key
|
|
||||||
func DefaultSession(providerType session.ProviderType, options ...session.ManagerConfigOpt) web.FilterChain {
|
|
||||||
return Session(providerType, defaultStorageKey, options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetDefaultStore call GetStore with default storage key
|
|
||||||
func GetDefaultStore(ctx *webContext.Context) (store session.Store, err error) {
|
|
||||||
return GetStore(ctx, defaultStorageKey)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -28,7 +28,6 @@ func TestSession(t *testing.T) {
|
|||||||
"*",
|
"*",
|
||||||
Session(
|
Session(
|
||||||
session.ProviderMemory,
|
session.ProviderMemory,
|
||||||
storeKey,
|
|
||||||
session.CfgCookieName(`go_session_id`),
|
session.CfgCookieName(`go_session_id`),
|
||||||
session.CfgSetCookie(true),
|
session.CfgSetCookie(true),
|
||||||
session.CfgGcLifeTime(3600),
|
session.CfgGcLifeTime(3600),
|
||||||
@ -56,14 +55,12 @@ func TestSession(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetStore(t *testing.T) {
|
func TestGetStore(t *testing.T) {
|
||||||
storeKey := uuid.New().String()
|
|
||||||
handler := web.NewControllerRegister()
|
handler := web.NewControllerRegister()
|
||||||
|
|
||||||
handler.InsertFilterChain(
|
handler.InsertFilterChain(
|
||||||
"*",
|
"*",
|
||||||
Session(
|
Session(
|
||||||
session.ProviderMemory,
|
session.ProviderMemory,
|
||||||
storeKey,
|
|
||||||
session.CfgCookieName(`go_session_id`),
|
session.CfgCookieName(`go_session_id`),
|
||||||
session.CfgSetCookie(true),
|
session.CfgSetCookie(true),
|
||||||
session.CfgGcLifeTime(3600),
|
session.CfgGcLifeTime(3600),
|
||||||
@ -86,98 +83,7 @@ func TestGetStore(t *testing.T) {
|
|||||||
c = context.Background()
|
c = context.Background()
|
||||||
)
|
)
|
||||||
|
|
||||||
if store, err = GetStore(ctx, storeKey); err == nil {
|
if store, err = GetStore(ctx); err == nil {
|
||||||
if store == nil {
|
|
||||||
t.Error(`store should not be nil`)
|
|
||||||
} else {
|
|
||||||
_ = store.Set(c, checkKey, checkValue)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
next(ctx)
|
|
||||||
|
|
||||||
if store != nil {
|
|
||||||
if v := store.Get(c, checkKey); v != checkValue {
|
|
||||||
t.Error(v, `is not equals to`, checkValue)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Error(`store should not be nil`)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
handler.Any("*", func(ctx *webContext.Context) {
|
|
||||||
ctx.Output.SetStatus(200)
|
|
||||||
})
|
|
||||||
|
|
||||||
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDefaultSession(t *testing.T) {
|
|
||||||
handler := web.NewControllerRegister()
|
|
||||||
handler.InsertFilterChain(
|
|
||||||
"*",
|
|
||||||
DefaultSession(
|
|
||||||
session.ProviderMemory,
|
|
||||||
session.CfgCookieName(`go_session_id`),
|
|
||||||
session.CfgSetCookie(true),
|
|
||||||
session.CfgGcLifeTime(3600),
|
|
||||||
session.CfgMaxLifeTime(3600),
|
|
||||||
session.CfgSecure(false),
|
|
||||||
session.CfgCookieLifeTime(3600),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
handler.InsertFilterChain(
|
|
||||||
"*",
|
|
||||||
func(next web.FilterFunc) web.FilterFunc {
|
|
||||||
return func(ctx *webContext.Context) {
|
|
||||||
if store := ctx.Input.GetData(defaultStorageKey); store == nil {
|
|
||||||
t.Error(`store should not be nil`)
|
|
||||||
}
|
|
||||||
next(ctx)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
handler.Any("*", func(ctx *webContext.Context) {
|
|
||||||
ctx.Output.SetStatus(200)
|
|
||||||
})
|
|
||||||
|
|
||||||
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetDefaultStore(t *testing.T) {
|
|
||||||
handler := web.NewControllerRegister()
|
|
||||||
|
|
||||||
handler.InsertFilterChain(
|
|
||||||
"*",
|
|
||||||
DefaultSession(
|
|
||||||
session.ProviderMemory,
|
|
||||||
session.CfgCookieName(`go_session_id`),
|
|
||||||
session.CfgSetCookie(true),
|
|
||||||
session.CfgGcLifeTime(3600),
|
|
||||||
session.CfgMaxLifeTime(3600),
|
|
||||||
session.CfgSecure(false),
|
|
||||||
session.CfgCookieLifeTime(3600),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
handler.InsertFilterChain(
|
|
||||||
"*",
|
|
||||||
func(next web.FilterFunc) web.FilterFunc {
|
|
||||||
return func(ctx *webContext.Context) {
|
|
||||||
var (
|
|
||||||
checkKey = `asodiuasdk1j)AS(87`
|
|
||||||
checkValue = `ASsd-09812-3`
|
|
||||||
|
|
||||||
store session.Store
|
|
||||||
err error
|
|
||||||
|
|
||||||
c = context.Background()
|
|
||||||
)
|
|
||||||
|
|
||||||
if store, err = GetDefaultStore(ctx); err == nil {
|
|
||||||
if store == nil {
|
if store == nil {
|
||||||
t.Error(`store should not be nil`)
|
t.Error(`store should not be nil`)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -279,7 +279,10 @@ func (manager *Manager) GetSessionStore(sid string) (sessions Store, err error)
|
|||||||
// it can do gc in times after gc lifetime.
|
// it can do gc in times after gc lifetime.
|
||||||
func (manager *Manager) GC() {
|
func (manager *Manager) GC() {
|
||||||
manager.provider.SessionGC(nil)
|
manager.provider.SessionGC(nil)
|
||||||
time.AfterFunc(time.Duration(manager.config.Gclifetime)*time.Second, func() { manager.GC() })
|
ticker := time.NewTicker(time.Duration(manager.config.Gclifetime) * time.Second)
|
||||||
|
for range ticker.C {
|
||||||
|
manager.provider.SessionGC(nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request.
|
// SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user