birthdaybot/server/server.go

39 lines
816 B
Go
Raw Normal View History

2023-06-29 22:46:17 +01:00
package server
import (
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
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
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-30 09:33:25 +01:00
type Server struct {
2023-06-30 09:38:29 +01:00
logger *log.Logger
2023-06-30 09:33:25 +01:00
config *model.Config
birthdays []*model.Birthday
}
func New(config *model.Config) (*Server, error) {
2023-06-30 09:38:29 +01:00
logger := log.New(os.Stderr)
logger.Info("Parsing CSV file", "birthdayFile", config.BirthdayFile)
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-06-30 09:38:29 +01:00
logger.Info("creating server")
2023-06-30 09:33:25 +01:00
server := &Server{
2023-06-30 09:38:29 +01:00
logger: logger,
2023-06-30 09:33:25 +01:00
config: config,
birthdays: birthdays,
}
return server, nil
2023-06-29 22:46:17 +01:00
}