Files
mtusi2026lab4-fork/src/backend/repositories/config.go
T

48 lines
1.1 KiB
Go

package repositories
import (
"errors"
"os"
"strconv"
"strings"
"time"
"github.com/redis/go-redis/v9"
)
func RedisClientFromEnvironment() (*redis.Client, error) {
if path := os.Getenv("CACHE_URL_FILE"); path != "" {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
options, err := redis.ParseURL(strings.TrimSpace(string(data)))
if err != nil {
return nil, errors.New("invalid Redis URL in CACHE_URL_FILE")
}
options.PoolSize = 10
options.MaxRetries = -1
options.DialTimeout = time.Second
options.ReadTimeout = time.Second
options.WriteTimeout = time.Second
return redis.NewClient(options), nil
}
address := os.Getenv("REDIS_ADDR")
if address == "" {
address = "redis:6379"
}
return NewRedisClient(address), nil
}
func IdempotencyTTL() (time.Duration, error) {
value := os.Getenv("IDEMPOTENCY_TTL_SECONDS")
if value == "" {
value = "86400"
}
seconds, err := strconv.ParseInt(value, 10, 64)
if err != nil || seconds < 1 || seconds > 31536000 {
return 0, errors.New("IDEMPOTENCY_TTL_SECONDS must be between 1 and 31536000")
}
return time.Duration(seconds) * time.Second, nil
}