chores: refactor struct and structure project

This commit is contained in:
ericprd
2025-03-05 21:21:44 +08:00
parent 13a8481f22
commit 567e0e32ca
103 changed files with 1125 additions and 731 deletions
+26 -2
View File
@@ -2,12 +2,36 @@ package utils
import (
"context"
"fmt"
"legalgo-BE-go/config"
"time"
"github.com/redis/go-redis/v9"
)
func StoreToken(ctx context.Context, rdb *redis.Client, token, username string) error {
func GetTokenRedis(ctx context.Context, rdb *redis.Client, id string) (string, error) {
val, err := rdb.Get(ctx, "token-"+id).Result()
switch {
case err == redis.Nil:
return "", fmt.Errorf("token does not exist")
case err != nil:
return "", err
case val == "":
return "", fmt.Errorf("token does not exist")
}
return val, nil
}
func StoreTokenRedis(ctx context.Context, rdb *redis.Client, token, id string) error {
_, err := rdb.Get(ctx, "token-"+id).Result()
if err != redis.Nil {
// If there's an existing token, delete it (expire the previous session)
if err := rdb.Del(ctx, "token-"+id).Err(); err != nil {
return err
}
}
// return rdb.Set(ctx context.Context, key string, value interface{}, expiration time.Duration)
return rdb.Set(ctx, "token"+username, token, time.Hour).Err()
return rdb.Set(ctx, "token-"+id, token, time.Minute*time.Duration(config.REDIS_TIMEOUT)).Err()
}