2017-06-16 10:33:35 +02:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
2017-10-25 00:36:16 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2018-04-13 16:44:23 +02:00
|
|
|
"dev.sum7.eu/genofire/logmania/bot"
|
|
|
|
"dev.sum7.eu/genofire/logmania/database"
|
|
|
|
"dev.sum7.eu/genofire/logmania/lib"
|
|
|
|
"dev.sum7.eu/genofire/logmania/notify"
|
2017-06-16 10:33:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
|
|
|
notify.Notifier
|
2017-08-09 08:45:45 +02:00
|
|
|
list []notify.Notifier
|
|
|
|
channelNotify chan *log.Entry
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:57:36 +01:00
|
|
|
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
2017-06-16 10:33:35 +02:00
|
|
|
var list []notify.Notifier
|
|
|
|
for _, init := range notify.NotifyRegister {
|
2017-11-10 18:57:36 +01:00
|
|
|
notify := init(config, db, bot)
|
2017-06-16 10:33:35 +02:00
|
|
|
|
|
|
|
if notify == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
list = append(list, notify)
|
|
|
|
}
|
2017-08-09 08:45:45 +02:00
|
|
|
|
|
|
|
n := &Notifier{
|
|
|
|
list: list,
|
|
|
|
channelNotify: make(chan *log.Entry),
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
2017-08-09 08:45:45 +02:00
|
|
|
go n.sender()
|
|
|
|
return n
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 08:45:45 +02:00
|
|
|
func (n *Notifier) sender() {
|
|
|
|
for c := range n.channelNotify {
|
|
|
|
for _, item := range n.list {
|
2017-11-10 18:57:36 +01:00
|
|
|
item.Send(c)
|
2017-08-09 08:45:45 +02:00
|
|
|
}
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:57:36 +01:00
|
|
|
func (n *Notifier) Send(e *log.Entry) error {
|
2017-08-09 08:45:45 +02:00
|
|
|
n.channelNotify <- e
|
2017-10-25 00:36:16 +02:00
|
|
|
return nil
|
2017-08-09 08:45:45 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 10:33:35 +02:00
|
|
|
func (n *Notifier) Close() {
|
|
|
|
for _, item := range n.list {
|
|
|
|
item.Close()
|
|
|
|
}
|
|
|
|
}
|