2023-06-29 22:46:17 +01:00
|
|
|
package server
|
|
|
|
|
2023-06-29 23:27:38 +01:00
|
|
|
import (
|
2023-07-01 17:00:48 +01:00
|
|
|
"errors"
|
2023-06-30 09:33:25 +01:00
|
|
|
"fmt"
|
2023-06-30 09:38:29 +01:00
|
|
|
"os"
|
2023-06-30 09:33:25 +01:00
|
|
|
|
2023-06-29 23:27:38 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
2023-07-01 17:00:48 +01:00
|
|
|
"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"
|
2023-06-29 23:27:38 +01:00
|
|
|
)
|
|
|
|
|
2023-07-01 17:00:48 +01:00
|
|
|
var ErrNoNotificationServices = errors.New("there are no notification services configured")
|
|
|
|
|
2023-06-30 09:33:25 +01:00
|
|
|
type Server struct {
|
2023-07-01 17:00:48 +01:00
|
|
|
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 := notification.NewTelegramNotificationService(logger, config.TelegramNotifications)
|
|
|
|
notificationServices = append(notificationServices, telegramNotificationService)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(notificationServices) == 0 {
|
|
|
|
return nil, ErrNoNotificationServices
|
|
|
|
}
|
|
|
|
|
|
|
|
return notificationServices, nil
|
2023-06-30 09:33:25 +01:00
|
|
|
}
|
2023-06-29 23:27:38 +01:00
|
|
|
|
|
|
|
func New(config *model.Config) (*Server, error) {
|
2023-06-30 09:38:29 +01:00
|
|
|
logger := log.New(os.Stderr)
|
|
|
|
|
2023-07-01 17:00:48 +01:00
|
|
|
logger.Debug("parsing CSV file", "birthdayFile", config.BirthdayFile)
|
2023-06-30 09:38:29 +01:00
|
|
|
|
2023-06-30 09:33:25 +01:00
|
|
|
birthdays, err := parser.ParseCSV(config.BirthdayFile)
|
|
|
|
if err != nil {
|
2023-06-30 09:38:29 +01:00
|
|
|
logger.Error("cannot parse CSV file", "birthdayFile", config.BirthdayFile, "error", err)
|
2023-06-30 09:33:25 +01:00
|
|
|
return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err)
|
|
|
|
}
|
|
|
|
|
2023-07-01 17:00:48 +01:00
|
|
|
logger.Debug("creating notification services from config")
|
|
|
|
|
|
|
|
notificationServices, err := createNotificationServices(logger, config)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error creating notification services", "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-30 09:38:29 +01:00
|
|
|
logger.Info("creating server")
|
|
|
|
|
2023-06-30 09:33:25 +01:00
|
|
|
server := &Server{
|
2023-07-01 17:00:48 +01:00
|
|
|
Logger: logger,
|
|
|
|
config: config,
|
|
|
|
birthdays: birthdays,
|
|
|
|
workers: []*Worker{NewWorker(logger)},
|
|
|
|
notificationServices: notificationServices,
|
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() {
|
2023-07-01 16:31:09 +01:00
|
|
|
s.Logger.Info("starting server")
|
2023-07-01 16:23:19 +01:00
|
|
|
for _, worker := range s.workers {
|
|
|
|
worker.Start()
|
|
|
|
}
|
2023-07-01 16:31:09 +01:00
|
|
|
s.Logger.Info("server started", "workers", len(s.workers))
|
2023-07-01 16:23:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() {
|
2023-07-01 16:31:09 +01:00
|
|
|
s.Logger.Info("stopping server")
|
2023-07-01 16:23:19 +01:00
|
|
|
for _, worker := range s.workers {
|
|
|
|
worker.Stop()
|
|
|
|
}
|
2023-07-01 16:31:09 +01:00
|
|
|
s.Logger.Info("server stopped", "workers", len(s.workers))
|
2023-07-01 16:23:19 +01:00
|
|
|
}
|