Add redis cluster client as a session store.
This commit is contained in:
parent
9970eb16c9
commit
f2f4c72aa6
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -11,3 +11,4 @@ data.db
|
|||
.DS_Store
|
||||
.env.local
|
||||
*.tar.gz
|
||||
.vscode/
|
|
@ -4,13 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type RedisStore struct {
|
||||
ctx context.Context
|
||||
store *redis.Client
|
||||
store RedisSessionClient
|
||||
}
|
||||
|
||||
// AddUserSession adds the user session to redis
|
||||
|
|
|
@ -3,6 +3,7 @@ package sessionstore
|
|||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/envstore"
|
||||
|
@ -121,6 +122,23 @@ func RemoveSocialLoginState(key string) {
|
|||
func InitSession() {
|
||||
if envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL) != "" {
|
||||
log.Println("using redis store to save sessions")
|
||||
if isCluster(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL)) {
|
||||
clusterOpt, err := getClusterOptions(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
|
||||
if err != nil {
|
||||
log.Fatalln("Error parsing redis url:", err)
|
||||
}
|
||||
rdb := redis.NewClusterClient(clusterOpt)
|
||||
ctx := context.Background()
|
||||
_, err = rdb.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
log.Fatalln("Error connecting to redis cluster server", err)
|
||||
}
|
||||
SessionStoreObj.RedisMemoryStoreObj = &RedisStore{
|
||||
ctx: ctx,
|
||||
store: rdb,
|
||||
}
|
||||
return
|
||||
}
|
||||
opt, err := redis.ParseURL(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyRedisURL))
|
||||
if err != nil {
|
||||
log.Fatalln("Error parsing redis url:", err)
|
||||
|
@ -144,3 +162,19 @@ func InitSession() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isCluster(url string) bool {
|
||||
return len(strings.Split(url, ",")) > 1
|
||||
}
|
||||
|
||||
func getClusterOptions(url string) (*redis.ClusterOptions, error) {
|
||||
hostPortsList := strings.Split(url, ",")
|
||||
opt, err := redis.ParseURL(hostPortsList[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
urls := []string{opt.Addr}
|
||||
urlList := hostPortsList[1:]
|
||||
urls = append(urls, urlList...)
|
||||
return &redis.ClusterOptions{Addrs: urls}, nil
|
||||
}
|
||||
|
|
18
server/sessionstore/session_client.go
Normal file
18
server/sessionstore/session_client.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package sessionstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type RedisSessionClient interface {
|
||||
HMSet(ctx context.Context, key string, values ...interface{}) *redis.BoolCmd
|
||||
Del(ctx context.Context, keys ...string) *redis.IntCmd
|
||||
HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
|
||||
HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd
|
||||
HGetAll(ctx context.Context, key string) *redis.StringStringMapCmd
|
||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
|
||||
Get(ctx context.Context, key string) *redis.StringCmd
|
||||
}
|
Loading…
Reference in New Issue
Block a user