logmania/notify/websocket/main.go

104 lines
2.0 KiB
Go
Raw Normal View History

2018-04-26 21:05:27 +02:00
package xmpp
import (
"net/http"
"dev.sum7.eu/genofire/golang-lib/websocket"
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/logmania/bot"
"dev.sum7.eu/genofire/logmania/database"
"dev.sum7.eu/genofire/logmania/lib"
"dev.sum7.eu/genofire/logmania/notify"
)
const (
proto = "ws"
)
var logger = log.WithField("notify", proto)
type Notifier struct {
notify.Notifier
defaults []*database.Notify
2018-04-26 21:05:27 +02:00
ws *websocket.Server
formatter log.Formatter
}
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
inputMSG := make(chan *websocket.Message)
ws := websocket.NewServer(inputMSG, nil)
http.HandleFunc("/ws", ws.Handler)
http.Handle("/", http.FileServer(http.Dir(config.Websocket.Webroot)))
go func() {
for msg := range inputMSG {
if msg.Subject != "bot" {
logger.Warnf("receive unknown websocket message: %s", msg.Subject)
continue
}
bot.Handle(func(answer string) {
msg.Answer("bot", answer)
}, "", msg.Body.(string))
}
}()
srv := &http.Server{
Addr: config.Websocket.Address,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}()
logger.WithField("http-socket", config.Websocket.Address).Info("startup")
var defaults []*database.Notify
if config.Websocket.Default != "" {
defaults = append(defaults, &database.Notify{
Protocol: proto,
To: config.Websocket.Default,
})
}
2018-04-26 21:05:27 +02:00
return &Notifier{
defaults: defaults,
ws: ws,
2018-04-26 21:05:27 +02:00
formatter: &log.TextFormatter{
DisableTimestamp: true,
},
}
}
func (n *Notifier) Default() []*database.Notify {
return n.defaults
}
2018-04-26 21:05:27 +02:00
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
if to.Protocol != proto {
return false
}
2018-05-18 14:04:54 +02:00
2018-04-26 21:05:27 +02:00
n.ws.SendAll(&websocket.Message{
Subject: to.Address(),
2018-05-18 14:04:54 +02:00
Body: &log.Entry{
Buffer: e.Buffer,
Data: e.Data,
Level: e.Level,
Logger: e.Logger,
Message: to.RunReplace(e.Message),
Time: e.Time,
},
2018-04-26 21:05:27 +02:00
})
return true
}
func (n *Notifier) Close() {
}
func init() {
notify.AddNotifier(Init)
}