[TASK] add notify to xmpp
This commit is contained in:
parent
8ca34866fd
commit
f94f35c657
|
@ -5,21 +5,25 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
"github.com/genofire/logmania/database"
|
"github.com/genofire/logmania/database"
|
||||||
"github.com/genofire/logmania/log"
|
"github.com/genofire/logmania/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/genofire/logmania/notify"
|
||||||
)
|
)
|
||||||
|
|
||||||
// http.Handler for init network
|
// http.Handler for init network
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
http.Handler
|
http.Handler
|
||||||
upgrader websocket.Upgrader
|
upgrader websocket.Upgrader
|
||||||
|
Notify notify.Notifier
|
||||||
}
|
}
|
||||||
|
|
||||||
// init new Handler
|
// init new Handler
|
||||||
func NewHandler() *Handler {
|
func NewHandler(notifyHandler notify.Notifier) *Handler {
|
||||||
return &Handler{
|
return &Handler{
|
||||||
upgrader: websocket.Upgrader{},
|
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)
|
logEntry.Error("umarshal log entry:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
database.InsertEntry(token, &entry)
|
dbEntry := database.InsertEntry(token, &entry)
|
||||||
|
if dbEntry != nil && h.Notify != nil {
|
||||||
|
h.Notify.Send(dbEntry)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
package receive
|
|
|
@ -20,6 +20,8 @@ import (
|
||||||
"github.com/genofire/logmania/lib"
|
"github.com/genofire/logmania/lib"
|
||||||
"github.com/genofire/logmania/log"
|
"github.com/genofire/logmania/log"
|
||||||
logOutput "github.com/genofire/logmania/log/hook/output"
|
logOutput "github.com/genofire/logmania/log/hook/output"
|
||||||
|
"github.com/genofire/logmania/notify"
|
||||||
|
"github.com/genofire/logmania/notify/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -27,6 +29,7 @@ var (
|
||||||
config *lib.Config
|
config *lib.Config
|
||||||
api *lib.HTTPServer
|
api *lib.HTTPServer
|
||||||
apiNoPanic *bool
|
apiNoPanic *bool
|
||||||
|
notifier notify.Notifier
|
||||||
debug bool
|
debug bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,9 +54,11 @@ func main() {
|
||||||
database.Connect(config.Database.Type, config.Database.Connect)
|
database.Connect(config.Database.Type, config.Database.Connect)
|
||||||
log.AddLogger("selflogger", logger)
|
log.AddLogger("selflogger", logger)
|
||||||
|
|
||||||
|
notifier = all.NotifyInit(&config.Notify)
|
||||||
|
|
||||||
api = &lib.HTTPServer{
|
api = &lib.HTTPServer{
|
||||||
Addr: config.API.Bind,
|
Addr: config.API.Bind,
|
||||||
Handler: receive.NewHandler(),
|
Handler: receive.NewHandler(notifier),
|
||||||
}
|
}
|
||||||
api.Start()
|
api.Start()
|
||||||
|
|
||||||
|
@ -76,6 +81,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func quit() {
|
func quit() {
|
||||||
|
notifier.Close()
|
||||||
log.Info("quit of logmania")
|
log.Info("quit of logmania")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{}
|
app := Application{}
|
||||||
db.Where("token = ?", token).First(&app)
|
db.Where("token = ?", token).First(&app)
|
||||||
entry := transformToDB(entryLog)
|
entry := transformToDB(entryLog)
|
||||||
|
@ -38,4 +38,5 @@ func InsertEntry(token string, entryLog *log.Entry) {
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
log.Error("saving log entry to database", result.Error)
|
log.Error("saving log entry to database", result.Error)
|
||||||
}
|
}
|
||||||
|
return entry
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,3 +12,9 @@ type User struct {
|
||||||
NotifyAfterLoglevel log.LogLevel
|
NotifyAfterLoglevel log.LogLevel
|
||||||
Permissions []Application `gorm:"many2many:user_permissions;"`
|
Permissions []Application `gorm:"many2many:user_permissions;"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UserByApplication(id int) []*User {
|
||||||
|
var users []*User
|
||||||
|
db.Model(&Application{ID: id}).Related(&users)
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,18 @@ type Config struct {
|
||||||
Bind string `toml:"bind"`
|
Bind string `toml:"bind"`
|
||||||
Interactive bool `toml:"interactive"`
|
Interactive bool `toml:"interactive"`
|
||||||
} `toml:"api"`
|
} `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 {
|
XMPP struct {
|
||||||
Host string `toml:"host"`
|
Host string `toml:"host"`
|
||||||
Username string `toml:"username"`
|
Username string `toml:"username"`
|
||||||
|
@ -26,15 +37,6 @@ type Config struct {
|
||||||
StatusMessage string `toml:"status_message"`
|
StatusMessage string `toml:"status_message"`
|
||||||
StartupNotify string `toml:"startup_notify"`
|
StartupNotify string `toml:"startup_notify"`
|
||||||
} `toml:"xmpp"`
|
} `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)
|
// read configuration from a file (use toml as file-format)
|
||||||
|
|
|
@ -10,7 +10,9 @@ var loggers = make(map[string]Logger)
|
||||||
|
|
||||||
// bind logger to handle saving/output of a Log entry
|
// bind logger to handle saving/output of a Log entry
|
||||||
func AddLogger(name string, logger Logger) {
|
func AddLogger(name string, logger Logger) {
|
||||||
|
if logger != nil {
|
||||||
loggers[name] = logger
|
loggers[name] = logger
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func RemoveLogger(name string) {
|
func RemoveLogger(name string) {
|
||||||
loggers[name].Close()
|
loggers[name].Close()
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package all
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/genofire/logmania/notify/xmpp"
|
||||||
|
)
|
|
@ -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) {
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package xmpp
|
||||||
|
|
||||||
|
import "github.com/genofire/logmania/database"
|
||||||
|
|
||||||
|
func FormatEntry(e *database.Entry) string {
|
||||||
|
return e.Text
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue