Files
app-api/internal/config/redis.go
T

57 lines
952 B
Go
Raw Normal View History

2026-05-21 15:20:12 +08:00
package config
import (
"fmt"
"os"
"strconv"
)
type RedisConfig struct {
Host string
Port int
Password string
Database int
}
func loadRedis(env string) RedisConfig {
if os.Getenv("REDIS_HOST") != "" {
return redisFromEnv()
}
switch env {
case "production":
return RedisConfig{
Host: "172.31.38.162",
Port: 16279,
Password: getEnv("REDIS_PASSWORD", "eafon123!"),
Database: 1,
}
default:
return RedisConfig{
2026-05-27 18:07:55 +08:00
Host: "localhost",
Port: 6379,
Password: "",
2026-05-21 15:20:12 +08:00
Database: 1,
}
}
}
func redisFromEnv() RedisConfig {
port, _ := strconv.Atoi(getEnv("REDIS_PORT", "16279"))
db, _ := strconv.Atoi(getEnv("REDIS_DATABASE", "1"))
return RedisConfig{
Host: os.Getenv("REDIS_HOST"),
Port: port,
Password: os.Getenv("REDIS_PASSWORD"),
Database: db,
}
}
func (r RedisConfig) validate() error {
if r.Host == "" {
return fmt.Errorf("redis host is required")
}
return nil
}