birthdaybot/server/server.go

110 lines
2.9 KiB
Go
Raw Normal View History

2023-06-29 22:46:17 +01:00
package server
import (
"errors"
2023-06-30 09:33:25 +01:00
"fmt"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
2023-06-30 09:33:25 +01:00
"git.ctrlz.es/mgdelacroix/birthdaybot/parser"
2023-06-30 09:38:29 +01:00
"github.com/charmbracelet/log"
)
var ErrNoNotificationServices = errors.New("there are no notification services configured")
2023-06-30 09:33:25 +01:00
type Server struct {
Logger *log.Logger
config *model.Config
workers []*Worker
birthdays []*model.Birthday
notificationServices []notification.NotificationService
}
func createNotificationServices(logger *log.Logger, config *model.Config) ([]notification.NotificationService, error) {
notificationServices := []notification.NotificationService{}
if config.TelegramNotifications != nil {
telegramNotificationService, err := notification.NewTelegramNotificationService(logger, config.TelegramNotifications)
if err != nil {
return nil, fmt.Errorf("cannot create telegram notification service: %w", err)
}
notificationServices = append(notificationServices, telegramNotificationService)
}
if len(notificationServices) == 0 {
return nil, ErrNoNotificationServices
}
return notificationServices, nil
2023-06-30 09:33:25 +01:00
}
2023-07-04 11:48:16 +01:00
func New(options ...Option) (*Server, error) {
srv := &Server{}
for _, option := range options {
srv = option(srv)
}
2023-06-30 09:38:29 +01:00
2023-07-04 11:48:16 +01:00
srv.Logger.Debug("parsing CSV file", "birthdayFile", srv.config.BirthdayFile)
2023-06-30 09:38:29 +01:00
2023-07-04 11:48:16 +01:00
birthdays, err := parser.ParseCSV(srv.config.BirthdayFile)
2023-06-30 09:33:25 +01:00
if err != nil {
2023-07-04 11:48:16 +01:00
srv.Logger.Error("cannot parse CSV file", "birthdayFile", srv.config.BirthdayFile, "error", err)
return nil, fmt.Errorf("cannot parse CSV file %s: %w", srv.config.BirthdayFile, err)
2023-06-30 09:33:25 +01:00
}
2023-07-04 11:48:16 +01:00
srv.Logger.Debug("creating notification services from config")
2023-07-04 11:48:16 +01:00
notificationServices, err := createNotificationServices(srv.Logger, srv.config)
if err != nil {
2023-07-04 11:48:16 +01:00
srv.Logger.Error("error creating notification services", "error", err)
return nil, err
}
2023-07-04 11:48:16 +01:00
srv.Logger.Info("creating server")
2023-06-30 09:38:29 +01:00
2023-06-30 09:33:25 +01:00
server := &Server{
2023-07-04 11:48:16 +01:00
Logger: srv.Logger,
config: srv.config,
birthdays: birthdays,
notificationServices: notificationServices,
2023-06-30 09:33:25 +01:00
}
server.workers = []*Worker{NewWorker(server)}
2023-06-30 09:33:25 +01:00
return server, nil
2023-06-29 22:46:17 +01:00
}
2023-07-01 16:23:19 +01:00
func (s *Server) Start() {
s.Logger.Info("starting server")
2023-07-01 16:23:19 +01:00
for _, worker := range s.workers {
worker.Start()
}
s.Logger.Info("server started", "workers", len(s.workers))
2023-07-01 16:23:19 +01:00
}
func (s *Server) Stop() {
s.Logger.Info("stopping server")
2023-07-01 16:23:19 +01:00
for _, worker := range s.workers {
worker.Stop()
}
s.Logger.Info("server stopped", "workers", len(s.workers))
2023-07-01 16:23:19 +01:00
}
func (s *Server) Notify(birthday *model.Birthday) error {
errs := []error{}
for _, service := range s.notificationServices {
err := service.Notify(birthday)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return errors.Join(errs...)
}
func (s *Server) Birthdays() []*model.Birthday {
return s.birthdays
}