135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"text/template"
|
|
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/parser"
|
|
"github.com/charmbracelet/log"
|
|
)
|
|
|
|
var (
|
|
ErrNoNotificationServices = errors.New("there are no notification services configured")
|
|
ErrNoLogger = errors.New("there is no logger configured")
|
|
ErrNoConfig = errors.New("configuration is required to create a server")
|
|
)
|
|
|
|
type Server struct {
|
|
Logger *log.Logger
|
|
config *model.Config
|
|
workers []Worker
|
|
birthdays []*model.Birthday
|
|
notificationServices []notification.NotificationService
|
|
tmpl *template.Template
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func New(options ...Option) (*Server, error) {
|
|
srv := &Server{}
|
|
for _, option := range options {
|
|
srv = option(srv)
|
|
}
|
|
|
|
if srv.Logger == nil {
|
|
return nil, ErrNoLogger
|
|
}
|
|
|
|
if srv.config == nil {
|
|
return nil, ErrNoConfig
|
|
}
|
|
|
|
if len(srv.birthdays) == 0 {
|
|
srv.Logger.Debug("parsing CSV file", "birthdayFile", srv.config.BirthdayFile)
|
|
|
|
var err error
|
|
srv.birthdays, err = parser.ParseCSV(srv.config.BirthdayFile)
|
|
if err != nil {
|
|
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)
|
|
}
|
|
}
|
|
|
|
if len(srv.notificationServices) == 0 {
|
|
srv.Logger.Debug("creating notification services from config")
|
|
|
|
var err error
|
|
srv.notificationServices, err = createNotificationServices(srv.Logger, srv.config)
|
|
if err != nil {
|
|
srv.Logger.Error("error creating notification services", "error", err)
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if len(srv.workers) == 0 {
|
|
srv.Logger.Debug("creating server workers")
|
|
|
|
srv.workers = []Worker{NewSimpleWorker(srv)}
|
|
}
|
|
|
|
if srv.config.BirthdayTemplate != "" {
|
|
srv.Logger.Debug("parsing birthday template", "file", srv.config.BirthdayTemplate)
|
|
|
|
var err error
|
|
srv.tmpl, err = template.ParseFiles(srv.config.BirthdayTemplate)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot parse template file %q: %w", srv.config.BirthdayTemplate, err)
|
|
}
|
|
}
|
|
|
|
return srv, nil
|
|
}
|
|
|
|
func (s *Server) Start() {
|
|
s.Logger.Info("starting server")
|
|
for _, worker := range s.workers {
|
|
worker.Start()
|
|
}
|
|
s.Logger.Debug("server started", "workers", len(s.workers))
|
|
}
|
|
|
|
func (s *Server) Stop() {
|
|
s.Logger.Info("stopping server")
|
|
for _, worker := range s.workers {
|
|
worker.Stop()
|
|
}
|
|
s.Logger.Debug("server stopped", "workers", len(s.workers))
|
|
}
|
|
|
|
func (s *Server) Notify(birthday *model.Birthday) error {
|
|
errs := []error{}
|
|
for _, service := range s.notificationServices {
|
|
err := service.Notify(birthday, s.tmpl)
|
|
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
|
|
}
|