package main import ( "context" "flag" "os" "os/signal" "git.faercol.me/faercol/telegram-notifier/notifier/notifier/config" "git.faercol.me/faercol/telegram-notifier/notifier/notifier/listener" "git.faercol.me/faercol/telegram-notifier/notifier/notifier/logger" "git.faercol.me/faercol/telegram-notifier/notifier/notifier/sender" ) type cliArgs struct { configPath string } func parseArgs() *cliArgs { configPath := flag.String("config", "", "Path to the JSON configuration file") flag.Parse() return &cliArgs{ configPath: *configPath, } } func main() { args := parseArgs() mainCtx, cancel := context.WithCancel(context.Background()) conf, err := config.New(args.configPath) if err != nil { panic(err) } logger.Init(conf.Log.Level) logger.L.Infof("Intialized logger with level %v", conf.Log.Level) logger.L.Debug("Initializing Telegram message sender") l, err := listener.New(conf.Listener.SockPath) if err != nil { logger.L.Fatalf("Failed to initialize listener: %s", err.Error()) } s := sender.New(conf, l.ExportChan()) go l.Run(mainCtx) go s.Run(mainCtx) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) logger.L.Info("Telegram message sender is operational") outerloop: for { select { case <-c: logger.L.Info("Stopping Telegram message sender") cancel() break outerloop case <-s.Exit(): logger.L.Fatal("Unexpected exit from the sender") case err := <-l.ErrorChan(): logger.L.Errorf("Unexpected error from listener: %s", err.Error()) case err := <-s.ErrChan(): logger.L.Errorf("Unexpected error from sender: %s", err.Error()) } } logger.L.Debug("Waiting for all services to shut down") <-s.Exit() logger.L.Info("Telegram message sender successfully stopped") os.Exit(0) }