Session 4: command dispatcher and main entrypoint

Wire Matrix messages to the *arr clients. Dispatcher parses
"<prefix><cmd> <query>", routes movie/tv/music to Radarr/Sonarr/Lidarr,
adds the top hit, and replies in a thread. Unconfigured services reply
with a clear message instead of failing.
This commit is contained in:
prosolis
2026-05-24 20:27:17 -07:00
parent 206b378d93
commit 0de6dd8c0d
4 changed files with 443 additions and 1 deletions

62
main.go Normal file
View File

@@ -0,0 +1,62 @@
package main
import (
"context"
"flag"
"log/slog"
"os"
"os/signal"
"syscall"
"bellhop/internal/arr"
"bellhop/internal/bot"
"bellhop/internal/config"
"bellhop/internal/matrix"
)
func main() {
configPath := flag.String("config", "config.yaml", "path to config file")
flag.Parse()
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})))
cfg, err := config.Load(*configPath)
if err != nil {
slog.Error("config load failed", "err", err)
os.Exit(1)
}
services := bot.Services{}
if cfg.Services.Radarr != nil {
services.Radarr = arr.NewRadarr(cfg.Services.Radarr)
}
if cfg.Services.Sonarr != nil {
services.Sonarr = arr.NewSonarr(cfg.Services.Sonarr)
}
if cfg.Services.Lidarr != nil {
services.Lidarr = arr.NewLidarr(cfg.Services.Lidarr)
}
mx, err := matrix.New(cfg.Matrix)
if err != nil {
slog.Error("matrix init failed", "err", err)
os.Exit(1)
}
dispatcher := bot.New(cfg.Matrix.CommandPrefix, services, mx)
mx.SetMessageHandler(dispatcher.Handle)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mx.Start(ctx)
slog.Info("bellhop started", "allowed_rooms", len(cfg.Matrix.AllowedRooms))
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
slog.Info("shutting down")
cancel()
mx.Stop()
}