redis-provider-fix
Some checks failed
deploy / deploy (push) Failing after 5s

This commit is contained in:
Untone 2024-02-22 11:51:20 +03:00
parent 8fca4cf4c0
commit 31079f2628
5 changed files with 41 additions and 43 deletions

View File

@ -3,8 +3,11 @@ package inmemory
import (
"fmt"
"os"
"errors"
log "github.com/sirupsen/logrus"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/memorystore/providers/redis"
)
// SetUserSession sets the user session for given user identifier in form recipe:user_id
@ -119,3 +122,25 @@ func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
}
return res.(bool), nil
}
// GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format,
// and assigns the JSON string to the provided user's ID.
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
redisURL := os.Getenv(constants.EnvKeyRedisURL)
if redisURL == "" {
return "", errors.New("Redis URL not found")
}
log.Info("Initializing Redis provider")
red, err := redis.NewRedisProvider(redisURL)
if err != nil {
return "", fmt.Errorf("failed to initialize Redis provider: %w", err)
}
combinedData, err := red.GetUserAppDataFromRedis(userId)
if err != nil {
return "", fmt.Errorf("failed to get Redis app data: %w", err)
}
return combinedData, nil
}

View File

@ -38,4 +38,6 @@ type Provider interface {
GetStringStoreEnvVariable(key string) (string, error)
// GetBoolStoreEnvVariable to get the bool env variable from env store
GetBoolStoreEnvVariable(key string) (bool, error)
GetUserAppDataFromRedis(userId string) (string, error)
}

View File

@ -222,22 +222,12 @@ func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
// GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format,
// and assigns the JSON string to the provided user's ID.
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
// Retrieve user data from Redis
key := fmt.Sprintf("user:%s:author", userId)
authorJSON, err := c.store.Get(c.ctx, key).Result()
if err != nil {
return "", err
}
key = fmt.Sprintf("user:%s:follows", userId)
followsJSON, err := c.store.Get(c.ctx, key).Result()
if err != nil {
return "", err
}
userProfile := c.store.Get(c.ctx, fmt.Sprintf(`user:%s:author`, userId))
userFollows := c.store.Get(c.ctx, fmt.Sprintf(`user:%s:follows`, userId))
// Combine user data into a JSON string
combinedData := fmt.Sprintf(`{"profile": %s, "follows": %s}`, authorJSON, followsJSON)
combinedData := fmt.Sprintf(`{"profile": %s, "follows": %s}`, userProfile, userFollows)
return combinedData, nil
}

View File

@ -5,7 +5,6 @@ import (
"fmt"
"strings"
"time"
"os"
"github.com/google/uuid"
@ -20,7 +19,6 @@ import (
mailService "github.com/authorizerdev/authorizer/server/email"
"github.com/authorizerdev/authorizer/server/graph/model"
"github.com/authorizerdev/authorizer/server/memorystore"
"github.com/authorizerdev/authorizer/server/memorystore/providers/redis"
"github.com/authorizerdev/authorizer/server/refs"
"github.com/authorizerdev/authorizer/server/smsproviders"
"github.com/authorizerdev/authorizer/server/token"
@ -318,19 +316,11 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
expiresIn = 1
}
redisURL := os.Getenv("REDIS_URL")
if redisURL != "" {
log.Info("Initializing Redis memory store")
RedisProvider, err := redis.NewRedisProvider(redisURL)
if err == nil {
appData, err := RedisProvider.GetUserAppDataFromRedis(user.ID)
appData, err := memorystore.Provider.GetUserAppDataFromRedis(user.ID)
if err == nil {
// Assign the combined data to the provided pointer
user.AppData = &appData
}
}
}
res = &model.AuthResponse{
Message: `Logged in successfully`,

View File

@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/google/uuid"
@ -15,7 +14,6 @@ import (
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/graph/model"
"github.com/authorizerdev/authorizer/server/memorystore"
"github.com/authorizerdev/authorizer/server/memorystore/providers/redis"
"github.com/authorizerdev/authorizer/server/token"
"github.com/authorizerdev/authorizer/server/utils"
)
@ -92,18 +90,11 @@ func SessionResolver(ctx context.Context, params *model.SessionQueryInput) (*mod
expiresIn = 1
}
redisURL := os.Getenv("REDIS_URL")
if redisURL != "" {
log.Info("Initializing Redis memory store")
RedisProvider, err := redis.NewRedisProvider(redisURL)
if err != nil {
appData, err := RedisProvider.GetUserAppDataFromRedis(user.ID)
appData, err := memorystore.Provider.GetUserAppDataFromRedis(user.ID)
if err == nil {
// Assign the combined data to the provided pointer
user.AppData = &appData
}
}
}
res = &model.AuthResponse{
Message: `Session token refreshed`,