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:
62
main.go
Normal file
62
main.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user