Adds server options
This commit is contained in:
parent
dee94e838d
commit
6c14403ff4
4 changed files with 59 additions and 23 deletions
|
@ -34,12 +34,13 @@ $ make run
|
|||
- [X] Define how to read the birthdays info
|
||||
- [X] Add a logger instance to the config
|
||||
- [ ] Add config validation on server creation
|
||||
- [ ] Add options to the server
|
||||
- [ ] Pass logger to the server through an option
|
||||
- [X] Add options to the server
|
||||
- [X] Pass logger to the server through an option
|
||||
- [ ] Configure logger through config (levels and such)
|
||||
- [ ] Reduce logger verbosity (through levels)
|
||||
- [ ] Create a configurable template to fill with each notification
|
||||
- [ ] Create different message systems to use with the bot
|
||||
- [X] Telegram
|
||||
- [ ] Email
|
||||
- [ ] Mattermost
|
||||
- [ ] Reduce logger verbosity
|
||||
- [ ] Enjoy!
|
||||
|
|
|
@ -11,24 +11,35 @@ import (
|
|||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
func configureLogger(config *model.Config, logger *log.Logger) {
|
||||
// ToDo: apply config to logger
|
||||
}
|
||||
|
||||
func main() {
|
||||
configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file")
|
||||
flag.Parse()
|
||||
|
||||
logger := log.New(os.Stderr)
|
||||
|
||||
config, err := model.ReadConfig(*configFlag)
|
||||
if err != nil {
|
||||
log.Error("error reading config", "configPath", *configFlag, "error", err)
|
||||
logger.Error("error reading config", "configPath", *configFlag, "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := config.IsValid(); err != nil {
|
||||
log.Error("invalid config", "configPath", *configFlag, "error", err)
|
||||
logger.Error("invalid config", "configPath", *configFlag, "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
srv, err := server.New(config)
|
||||
configureLogger(config, logger)
|
||||
|
||||
srv, err := server.New(
|
||||
server.WithLogger(logger),
|
||||
server.WithConfig(config),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("error creating server", "error", err)
|
||||
logger.Error("error creating server", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
22
server/options.go
Normal file
22
server/options.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type Option func(*Server) *Server
|
||||
|
||||
func WithConfig(config *model.Config) Option {
|
||||
return func(server *Server) *Server {
|
||||
server.config = config
|
||||
return server
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(logger *log.Logger) Option {
|
||||
return func(server *Server) *Server {
|
||||
server.Logger = logger
|
||||
return server
|
||||
}
|
||||
}
|
|
@ -3,7 +3,6 @@ package server
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
|
||||
|
@ -39,30 +38,33 @@ func createNotificationServices(logger *log.Logger, config *model.Config) ([]not
|
|||
return notificationServices, nil
|
||||
}
|
||||
|
||||
func New(config *model.Config) (*Server, error) {
|
||||
logger := log.New(os.Stderr)
|
||||
|
||||
logger.Debug("parsing CSV file", "birthdayFile", config.BirthdayFile)
|
||||
|
||||
birthdays, err := parser.ParseCSV(config.BirthdayFile)
|
||||
if err != nil {
|
||||
logger.Error("cannot parse CSV file", "birthdayFile", config.BirthdayFile, "error", err)
|
||||
return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err)
|
||||
func New(options ...Option) (*Server, error) {
|
||||
srv := &Server{}
|
||||
for _, option := range options {
|
||||
srv = option(srv)
|
||||
}
|
||||
|
||||
logger.Debug("creating notification services from config")
|
||||
srv.Logger.Debug("parsing CSV file", "birthdayFile", srv.config.BirthdayFile)
|
||||
|
||||
notificationServices, err := createNotificationServices(logger, config)
|
||||
birthdays, err := parser.ParseCSV(srv.config.BirthdayFile)
|
||||
if err != nil {
|
||||
logger.Error("error creating notification services", "error", err)
|
||||
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)
|
||||
}
|
||||
|
||||
srv.Logger.Debug("creating notification services from config")
|
||||
|
||||
notificationServices, err := createNotificationServices(srv.Logger, srv.config)
|
||||
if err != nil {
|
||||
srv.Logger.Error("error creating notification services", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("creating server")
|
||||
srv.Logger.Info("creating server")
|
||||
|
||||
server := &Server{
|
||||
Logger: logger,
|
||||
config: config,
|
||||
Logger: srv.Logger,
|
||||
config: srv.config,
|
||||
birthdays: birthdays,
|
||||
notificationServices: notificationServices,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue