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

This commit is contained in:
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
}