add storeKey supports

This commit is contained in:
Anker Jam 2021-01-09 11:21:29 +08:00
parent 833d734921
commit 962eac05f6
2 changed files with 130 additions and 18 deletions

View File

@ -9,25 +9,30 @@ import (
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" "github.com/google/uuid"
"sync"
) )
var ( var (
sessionKey string sessionFormatSign = uuid.New().ID()
sessionKeyOnce sync.Once defaultStorageKey = uuid.New().String()
) )
func getSessionKey() string { func sessionStoreKey(key string) string {
return fmt.Sprintf(
sessionKeyOnce.Do(func() { `sess_%d:%s`,
//generate an unique session store key sessionFormatSign,
sessionKey = fmt.Sprintf(`sess_store:%d`, uuid.New().ID()) key,
}) )
return sessionKey
} }
func Session(providerType session.ProviderType, options ...session.ManagerConfigOpt) web.FilterChain { //Session maintain session for web service
//Session new a session storage and store it into webContext.Context
//
//params:
//ctx: pointer of beego web context
//storeName: set the storage key in ctx.Input
//
//if you want to get session storage, just see GetStore
func Session(providerType session.ProviderType, storeName string, 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()
@ -40,7 +45,7 @@ func Session(providerType session.ProviderType, options ...session.ManagerConfig
} 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(getSessionKey(), sess) ctx.Input.SetData(sessionStoreKey(storeName), sess)
} }
next(ctx) next(ctx)
@ -49,13 +54,14 @@ func Session(providerType session.ProviderType, options ...session.ManagerConfig
} }
} }
func GetStore(ctx *webContext.Context) (store session.Store, err error) { //GetStore get session storage in beego web context
func GetStore(ctx *webContext.Context, storeName string) (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(getSessionKey()).(session.Store); ok { if s, ok := ctx.Input.GetData(sessionStoreKey(storeName)).(session.Store); ok {
store = s store = s
return return
} else { } else {
@ -63,3 +69,13 @@ func GetStore(ctx *webContext.Context) (store session.Store, err error) {
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)
}

View File

@ -5,6 +5,7 @@ import (
"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"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -21,11 +22,13 @@ func testRequest(t *testing.T, handler *web.ControllerRegister, path string, met
} }
func TestSession(t *testing.T) { func TestSession(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),
@ -38,7 +41,7 @@ func TestSession(t *testing.T) {
"*", "*",
func(next web.FilterFunc) web.FilterFunc { func(next web.FilterFunc) web.FilterFunc {
return func(ctx *webContext.Context) { return func(ctx *webContext.Context) {
if store := ctx.Input.GetData(getSessionKey()); store == nil { if store := ctx.Input.GetData(storeKey); store == nil {
t.Error(`store should not be nil`) t.Error(`store should not be nil`)
} }
next(ctx) next(ctx)
@ -53,11 +56,104 @@ 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,
storeKey,
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 = GetStore(ctx, storeKey); 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.ProviderMemory,
session.CfgCookieName(`go_session_id`), session.CfgCookieName(`go_session_id`),
session.CfgSetCookie(true), session.CfgSetCookie(true),
@ -81,7 +177,7 @@ func TestGetStore(t *testing.T) {
c = context.Background() c = context.Background()
) )
if store, err = GetStore(ctx); err == nil { 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 {
@ -97,7 +193,7 @@ func TestGetStore(t *testing.T) {
if v := store.Get(c, checkKey); v != checkValue { if v := store.Get(c, checkKey); v != checkValue {
t.Error(v, `is not equals to`, checkValue) t.Error(v, `is not equals to`, checkValue)
} }
}else{ } else {
t.Error(`store should not be nil`) t.Error(`store should not be nil`)
} }