make session easier to be configured

This commit is contained in:
Anker Jam 2021-01-02 21:55:12 +08:00
parent adb4dabb24
commit 41b1833898
5 changed files with 173 additions and 38 deletions

View File

@ -15,21 +15,22 @@ import (
) )
func TestRedis(t *testing.T) { func TestRedis(t *testing.T) {
sessionConfig := &session.ManagerConfig{
CookieName: "gosessionid",
EnableSetCookie: true,
Gclifetime: 3600,
Maxlifetime: 3600,
Secure: false,
CookieLifeTime: 3600,
}
redisAddr := os.Getenv("REDIS_ADDR") redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" { if redisAddr == "" {
redisAddr = "127.0.0.1:6379" redisAddr = "127.0.0.1:6379"
} }
redisConfig := fmt.Sprintf("%s,100,,0,30", redisAddr)
sessionConfig := session.NewManagerConfig(
session.CfgCookieName(`gosessionid`),
session.CfgSetCookie(true),
session.CfgGcLifeTime(3600),
session.CfgMaxLifeTime(3600),
session.CfgSecure(false),
session.CfgCookieLifeTime(3600),
session.CfgProviderConfig(redisConfig),
)
sessionConfig.ProviderConfig = fmt.Sprintf("%s,100,,0,30", redisAddr)
globalSession, err := session.NewManager("redis", sessionConfig) globalSession, err := session.NewManager("redis", sessionConfig)
if err != nil { if err != nil {
t.Fatal("could not create manager:", err) t.Fatal("could not create manager:", err)

View File

@ -13,15 +13,15 @@ import (
) )
func TestRedisSentinel(t *testing.T) { func TestRedisSentinel(t *testing.T) {
sessionConfig := &session.ManagerConfig{ sessionConfig := session.NewManagerConfig(
CookieName: "gosessionid", session.CfgCookieName(`gosessionid`),
EnableSetCookie: true, session.CfgSetCookie(true),
Gclifetime: 3600, session.CfgGcLifeTime(3600),
Maxlifetime: 3600, session.CfgMaxLifeTime(3600),
Secure: false, session.CfgSecure(false),
CookieLifeTime: 3600, session.CfgCookieLifeTime(3600),
ProviderConfig: "127.0.0.1:6379,100,,0,master", session.CfgProviderConfig("127.0.0.1:6379,100,,0,master"),
} )
globalSessions, e := session.NewManager("redis_sentinel", sessionConfig) globalSessions, e := session.NewManager("redis_sentinel", sessionConfig)
if e != nil { if e != nil {
t.Log(e) t.Log(e)

View File

@ -91,25 +91,6 @@ func GetProvider(name string) (Provider, error) {
return provider, nil return provider, nil
} }
// ManagerConfig define the session config
type ManagerConfig struct {
CookieName string `json:"cookieName"`
EnableSetCookie bool `json:"enableSetCookie,omitempty"`
Gclifetime int64 `json:"gclifetime"`
Maxlifetime int64 `json:"maxLifetime"`
DisableHTTPOnly bool `json:"disableHTTPOnly"`
Secure bool `json:"secure"`
CookieLifeTime int `json:"cookieLifeTime"`
ProviderConfig string `json:"providerConfig"`
Domain string `json:"domain"`
SessionIDLength int64 `json:"sessionIDLength"`
EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
SessionIDPrefix string `json:"sessionIDPrefix"`
CookieSameSite http.SameSite `json:"cookieSameSite"`
}
// Manager contains Provider and its configuration. // Manager contains Provider and its configuration.
type Manager struct { type Manager struct {
provider Provider provider Provider

View File

@ -0,0 +1,137 @@
package session
import "net/http"
// ManagerConfig define the session config
type ManagerConfig struct {
CookieName string `json:"cookieName"`
EnableSetCookie bool `json:"enableSetCookie,omitempty"`
Gclifetime int64 `json:"gclifetime"`
Maxlifetime int64 `json:"maxLifetime"`
DisableHTTPOnly bool `json:"disableHTTPOnly"`
Secure bool `json:"secure"`
CookieLifeTime int `json:"cookieLifeTime"`
ProviderConfig string `json:"providerConfig"`
Domain string `json:"domain"`
SessionIDLength int64 `json:"sessionIDLength"`
EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
SessionIDPrefix string `json:"sessionIDPrefix"`
CookieSameSite http.SameSite `json:"cookieSameSite"`
}
type ManagerConfigOpt func(config *ManagerConfig)
func NewManagerConfig(opts ...ManagerConfigOpt) *ManagerConfig {
config := &ManagerConfig{}
for _, opt := range opts {
opt(config)
}
return config
}
// CfgCookieName set key of session id
func CfgCookieName(cookieName string) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.CookieName = cookieName
}
}
// CfgCookieName set len of session id
func CfgSessionIdLength(len int64) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.SessionIDLength = len
}
}
// CfgSessionIdPrefix set prefix of session id
func CfgSessionIdPrefix(prefix string) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.SessionIDPrefix = prefix
}
}
//CfgSetCookie whether set `Set-Cookie` header in HTTP response
func CfgSetCookie(enable bool) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.EnableSetCookie = enable
}
}
//CfgGcLifeTime set session gc lift time
func CfgGcLifeTime(lifeTime int64) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.Gclifetime = lifeTime
}
}
//CfgMaxLifeTime set session lift time
func CfgMaxLifeTime(lifeTime int64) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.Maxlifetime = lifeTime
}
}
//CfgGcLifeTime set session lift time
func CfgCookieLifeTime(lifeTime int) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.CookieLifeTime = lifeTime
}
}
//CfgProviderConfig configure session provider
func CfgProviderConfig(providerConfig string) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.ProviderConfig = providerConfig
}
}
//CfgDomain set cookie domain
func CfgDomain(domain string) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.Domain = domain
}
}
//CfgSessionIdInHTTPHeader enable session id in http header
func CfgSessionIdInHTTPHeader(enable bool) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.EnableSidInHTTPHeader = enable
}
}
//CfgSetSessionNameInHTTPHeader set key of session id in http header
func CfgSetSessionNameInHTTPHeader(name string) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.SessionNameInHTTPHeader = name
}
}
//EnableSidInURLQuery enable session id in query string
func CfgEnableSidInURLQuery(enable bool) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.EnableSidInURLQuery = enable
}
}
//DisableHTTPOnly set HTTPOnly for http.Cookie
func CfgHTTPOnly(HTTPOnly bool) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.DisableHTTPOnly = !HTTPOnly
}
}
//CfgSecure set Secure for http.Cookie
func CfgSecure(Enable bool) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.Secure = Enable
}
}
//CfgSameSite set http.SameSite
func CfgSameSite(sameSite http.SameSite) ManagerConfigOpt {
return func(config *ManagerConfig) {
config.CookieSameSite = sameSite
}
}

View File

@ -0,0 +1,16 @@
package session
const (
ProviderCookie = `cookie`
ProviderFile = `file`
ProviderMemory = `memory`
ProviderCouchbase = `couchbase`
ProviderLedis = `ledis`
ProviderMemcache = `memcache`
ProviderMysql = `mysql`
ProviderPostgresql = `postgresql`
ProviderRedis = `redis`
ProviderRedisCluster = `redis_cluster`
ProviderRedisSentinel = `redis_sentinel`
ProviderSsdb = `ssdb`
)