[TASK] add notify to xmpp

This commit is contained in:
Martin Geno 2017-06-16 10:33:35 +02:00
parent 8ca34866fd
commit f94f35c657
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
12 changed files with 161 additions and 20 deletions

View File

@ -5,21 +5,25 @@ import (
"encoding/json"
"net/http"
"github.com/gorilla/websocket"
"github.com/genofire/logmania/database"
"github.com/genofire/logmania/log"
"github.com/gorilla/websocket"
"github.com/genofire/logmania/notify"
)
// http.Handler for init network
type Handler struct {
http.Handler
upgrader websocket.Upgrader
Notify notify.Notifier
}
// init new Handler
func NewHandler() *Handler {
func NewHandler(notifyHandler notify.Notifier) *Handler {
return &Handler{
upgrader: websocket.Upgrader{},
Notify: notifyHandler,
}
}
@ -72,6 +76,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logEntry.Error("umarshal log entry:", err)
break
}
database.InsertEntry(token, &entry)
dbEntry := database.InsertEntry(token, &entry)
if dbEntry != nil && h.Notify != nil {
h.Notify.Send(dbEntry)
}
}
}

View File

@ -1 +0,0 @@
package receive

View File

@ -20,6 +20,8 @@ import (
"github.com/genofire/logmania/lib"
"github.com/genofire/logmania/log"
logOutput "github.com/genofire/logmania/log/hook/output"
"github.com/genofire/logmania/notify"
"github.com/genofire/logmania/notify/all"
)
var (
@ -27,6 +29,7 @@ var (
config *lib.Config
api *lib.HTTPServer
apiNoPanic *bool
notifier notify.Notifier
debug bool
)
@ -51,9 +54,11 @@ func main() {
database.Connect(config.Database.Type, config.Database.Connect)
log.AddLogger("selflogger", logger)
notifier = all.NotifyInit(&config.Notify)
api = &lib.HTTPServer{
Addr: config.API.Bind,
Handler: receive.NewHandler(),
Handler: receive.NewHandler(notifier),
}
api.Start()
@ -76,6 +81,7 @@ func main() {
}
func quit() {
notifier.Close()
log.Info("quit of logmania")
os.Exit(0)
}

View File

@ -28,7 +28,7 @@ func transformToDB(dbEntry *log.Entry) *Entry {
}
}
func InsertEntry(token string, entryLog *log.Entry) {
func InsertEntry(token string, entryLog *log.Entry) *Entry {
app := Application{}
db.Where("token = ?", token).First(&app)
entry := transformToDB(entryLog)
@ -38,4 +38,5 @@ func InsertEntry(token string, entryLog *log.Entry) {
if result.Error != nil {
log.Error("saving log entry to database", result.Error)
}
return entry
}

View File

@ -12,3 +12,9 @@ type User struct {
NotifyAfterLoglevel log.LogLevel
Permissions []Application `gorm:"many2many:user_permissions;"`
}
func UserByApplication(id int) []*User {
var users []*User
db.Model(&Application{ID: id}).Related(&users)
return users
}

View File

@ -14,7 +14,18 @@ type Config struct {
Bind string `toml:"bind"`
Interactive bool `toml:"interactive"`
} `toml:"api"`
Notify struct {
Notify NotifyConfig `toml:"notify"`
Database struct {
Type string `toml:"type"`
Connect string `toml:"connect"`
} `toml:"database"`
Webserver struct {
Enable bool `toml:"enable"`
Bind string `toml:"bind"`
} `toml:"webserver"`
}
type NotifyConfig struct {
XMPP struct {
Host string `toml:"host"`
Username string `toml:"username"`
@ -26,15 +37,6 @@ type Config struct {
StatusMessage string `toml:"status_message"`
StartupNotify string `toml:"startup_notify"`
} `toml:"xmpp"`
} `toml:"notify"`
Database struct {
Type string `toml:"type"`
Connect string `toml:"connect"`
} `toml:"database"`
Webserver struct {
Enable bool `toml:"enable"`
Bind string `toml:"bind"`
} `toml:"webserver"`
}
// read configuration from a file (use toml as file-format)

View File

@ -10,7 +10,9 @@ var loggers = make(map[string]Logger)
// bind logger to handle saving/output of a Log entry
func AddLogger(name string, logger Logger) {
if logger != nil {
loggers[name] = logger
}
}
func RemoveLogger(name string) {
loggers[name].Close()

39
notify/all/internal.go Normal file
View File

@ -0,0 +1,39 @@
package all
import (
"github.com/genofire/logmania/database"
"github.com/genofire/logmania/lib"
"github.com/genofire/logmania/notify"
)
type Notifier struct {
notify.Notifier
list []notify.Notifier
}
func NotifyInit(config *lib.NotifyConfig) notify.Notifier {
var list []notify.Notifier
for _, init := range notify.NotifyRegister {
notify := init(config)
if notify == nil {
continue
}
list = append(list, notify)
}
return &Notifier{
list: list,
}
}
func (n *Notifier) Send(entry *database.Entry) {
for _, item := range n.list {
item.Send(entry)
}
}
func (n *Notifier) Close() {
for _, item := range n.list {
item.Close()
}
}

5
notify/all/main.go Normal file
View File

@ -0,0 +1,5 @@
package all
import (
_ "github.com/genofire/logmania/notify/xmpp"
)

22
notify/main.go Normal file
View File

@ -0,0 +1,22 @@
package notify
import (
"github.com/genofire/logmania/database"
"github.com/genofire/logmania/lib"
)
var NotifyRegister []NotifyInit
type Notifier interface {
Send(entry *database.Entry)
Close()
}
type NotifyInit func(*lib.NotifyConfig) Notifier
func AddNotifier(n NotifyInit) {
NotifyRegister = append(NotifyRegister, n)
}
func Start(config *lib.NotifyConfig) {
}

7
notify/xmpp/internal.go Normal file
View File

@ -0,0 +1,7 @@
package xmpp
import "github.com/genofire/logmania/database"
func FormatEntry(e *database.Entry) string {
return e.Text
}

45
notify/xmpp/main.go Normal file
View File

@ -0,0 +1,45 @@
package xmpp
import (
"github.com/genofire/logmania/database"
"github.com/genofire/logmania/lib"
"github.com/genofire/logmania/log"
"github.com/genofire/logmania/notify"
xmpp "github.com/mattn/go-xmpp"
)
type Notifier struct {
notify.Notifier
client *xmpp.Client
}
func NotifyInit(config *lib.NotifyConfig) notify.Notifier {
options := xmpp.Options{
Host: config.XMPP.Host,
User: config.XMPP.Username,
Password: config.XMPP.Password,
NoTLS: config.XMPP.NoTLS,
Debug: config.XMPP.Debug,
Session: config.XMPP.Session,
Status: config.XMPP.Status,
StatusMessage: config.XMPP.StatusMessage,
}
client, err := options.NewClient()
if err != nil {
return nil
}
return &Notifier{client: client}
}
func (n *Notifier) Send(e *database.Entry) {
users := database.UserByApplication(e.ApplicationID)
for _, user := range users {
if user.NotifyXMPP && user.NotifyAfterLoglevel <= log.LogLevel(e.Level) {
n.client.SendHtml(xmpp.Chat{Remote: user.XMPP, Type: "chat", Text: FormatEntry(e)})
}
}
}
func init() {
notify.AddNotifier(NotifyInit)
}