diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-03-14 14:43:24 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-03-14 14:43:24 -0700 |
| commit | 2cf9f33ef42188dd35eaf4905cd2f05557aecca1 (patch) | |
| tree | e2cb7db1ccd7a4defae39395b76b144d66aacf2c /cmd | |
| parent | 0003a0912b04308fbbdfcb2801d0373e6d4de2f0 (diff) | |
Refactor ban management behind BanMgr interface
Extract ban logic into a BanMgr interface with two implementations:
- BanFile: file-based YAML storage with support for IP, username, and
nickname bans (backwards-compatible with legacy format)
- RedisBanMgr: Redis-backed implementation with permanent and temporary
ban support, using fail-safe deny-on-error behavior
This replaces scattered Redis calls in API handlers, transaction
handlers, and server connection logic with unified interface calls,
removing the Redis dependency from the API server constructor and
enabling ban functionality for both file-only and Redis deployments.
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/mobius-hotline-server/main.go | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go index 659ad13..8b9d54c 100644 --- a/cmd/mobius-hotline-server/main.go +++ b/cmd/mobius-hotline-server/main.go @@ -16,6 +16,7 @@ import ( "github.com/jhalter/mobius/hotline" "github.com/jhalter/mobius/internal/mobius" "github.com/oleksandr/bonjour" + "github.com/redis/go-redis/v9" ) //go:embed mobius/config @@ -114,10 +115,29 @@ func main() { os.Exit(1) } - srv.BanList, err = mobius.NewBanFile(path.Join(*configDir, "Banlist.yaml")) - if err != nil { - slogger.Error("Error loading ban list", "err", err) - os.Exit(1) + // Initialize ban list - use Redis if configured, otherwise use file-based storage + if *redisAddr != "" { + redisClient := redis.NewClient(&redis.Options{ + Addr: *redisAddr, + Password: *redisPassword, + DB: *redisDB, + }) + + // Verify Redis connection + if err := redisClient.Ping(ctx).Err(); err != nil { + slogger.Error("Error connecting to Redis", "err", err) + os.Exit(1) + } + + srv.Redis = redisClient + srv.BanList = mobius.NewRedisBanMgr(redisClient, slogger) + slogger.Info("Using Redis for ban management", "addr", *redisAddr) + } else { + srv.BanList, err = mobius.NewBanFile(path.Join(*configDir, "Banlist.yaml")) + if err != nil { + slogger.Error("Error loading ban list", "err", err) + os.Exit(1) + } } srv.ThreadedNewsMgr, err = mobius.NewThreadedNewsYAML(path.Join(*configDir, "ThreadedNews.yaml")) @@ -150,8 +170,11 @@ func main() { slogger.Error("Error reloading news", "err", err) } - if err := srv.BanList.(*mobius.BanFile).Load(); err != nil { - slogger.Error("Error reloading ban list", "err", err) + // Only reload ban list if using file-based storage (Redis doesn't need reload) + if banFile, ok := srv.BanList.(*mobius.BanFile); ok { + if err := banFile.Load(); err != nil { + slogger.Error("Error reloading ban list", "err", err) + } } if err := srv.ThreadedNewsMgr.(*mobius.ThreadedNewsYAML).Load(); err != nil { @@ -171,7 +194,7 @@ func main() { } if *apiAddr != "" { - sh := mobius.NewAPIServer(srv, reloadFunc, slogger, *apiKey, *redisAddr, *redisPassword, *redisDB) + sh := mobius.NewAPIServer(srv, reloadFunc, slogger, *apiKey) go sh.Serve(*apiAddr) } |