-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
67 lines (53 loc) · 1.4 KB
/
notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package notifier
import (
"sync"
"github.com/joaosoft/web"
"github.com/joaosoft/logger"
"github.com/joaosoft/manager"
)
type Notifier struct {
config *NotifierConfig
webClient *web.Client
logger logger.ILogger
isLogExternal bool
pm *manager.Manager
mux sync.Mutex
}
type INotifier interface {
Name() string
Notify(message string) error
}
// New ...
func New(options ...NotificationsOption) (*Notifier, error) {
config, simpleConfig, err := NewConfig()
webClient, err := web.NewClient()
if err != nil {
return nil, err
}
service := &Notifier{
pm: manager.NewManager(manager.WithRunInBackground(true)),
logger: logger.NewLogDefault("notifier", logger.WarnLevel),
config: config.Notifier,
webClient: webClient,
}
if err != nil {
service.logger.Error(err.Error())
} else if config.Notifier != nil {
service.pm.AddConfig("config_app", simpleConfig)
level, _ := logger.ParseLevel(config.Notifier.Log.Level)
service.logger.Debugf("setting log level to %s", level)
service.logger.Reconfigure(logger.WithLevel(level))
}
if service.isLogExternal {
service.pm.Reconfigure(manager.WithLogger(service.logger))
}
service.Reconfigure(options...)
return service, nil
}
func (n *Notifier) NewSlackNotifier(config ...*SlackConfig) INotifier {
c := n.config.Slack
if len(config) > 0 {
c = config[0]
}
return newNotifierSlack(n, c)
}