Files
uptimesebi/internal/platform/redis.go
T
Sebastian UnterschützandClaude Opus 5.5 d9891bb050 feat!: let checkers and status page servers reach data only through the api
Only cmd/api connects to Postgres and Redis. Agents authenticate with
tokens created by 'api agent create' and use /agent/v1: checkers long
poll for jobs, which doubles as heartbeat, and report results in
batches. Organizations manage monitors through /api/v1/org/monitors.

BREAKING CHANGE: cmd/checker and cmd/statuspage no longer read
UPTIME_DATABASE_URL or UPTIME_REDIS_*; they need UPTIME_API_URL and
UPTIME_AGENT_TOKEN. UPTIME_CHECKER_REGION is replaced by the region bound
to the agent token.

Closes #10
Closes #11
Closes #12
Closes #13
Closes #49

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 01:12:53 +01:00

57 lines
1.3 KiB
Go

package platform
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
)
// OpenRedis connects to a single server or, when c.RedisSentinel is enabled,
// to the current master via sentinel with automatic failover.
func OpenRedis(ctx context.Context, c config.Storage) (*redis.Client, error) {
var rdb *redis.Client
if c.RedisSentinel.Enabled() {
opts, err := failoverOptions(c.RedisURL, c.RedisSentinel)
if err != nil {
return nil, fmt.Errorf("redis: %w", err)
}
rdb = redis.NewFailoverClient(opts)
} else {
opts, err := redis.ParseURL(c.RedisURL)
if err != nil {
return nil, fmt.Errorf("redis: %w", err)
}
rdb = redis.NewClient(opts)
}
if err := rdb.Ping(ctx).Err(); err != nil {
_ = rdb.Close()
return nil, fmt.Errorf("redis: %w", err)
}
return rdb, nil
}
func failoverOptions(url string, s config.Sentinel) (*redis.FailoverOptions, error) {
fo := &redis.FailoverOptions{
MasterName: s.Master,
SentinelAddrs: s.Addrs,
SentinelUsername: s.Username,
SentinelPassword: s.Password,
}
if url == "" {
return fo, nil
}
base, err := redis.ParseURL(url)
if err != nil {
return nil, err
}
fo.Username = base.Username
fo.Password = base.Password
fo.DB = base.DB
fo.TLSConfig = base.TLSConfig
fo.Protocol = base.Protocol
return fo, nil
}