Add XMPP support and watching log into web page

This commit is contained in:
chteufleur 2016-04-19 20:36:38 +02:00
parent ea30512e65
commit a601d25b35
4 changed files with 48 additions and 12 deletions

View File

@ -19,7 +19,7 @@ type WebSocketController struct {
func (c *WebSocketController) Prepare() {
sess := c.GetSession(variables.SessionName)
if sess == nil {
c.Redirect(variables.LoginRouteNoRegex+variables.UserRoute, 302)
c.Redirect(variables.LoginRouteNoRegex+variables.ViewLogRoute, 302)
} else {
c.Data["IsAuthentificated"] = true
}

28
main.go
View File

@ -7,6 +7,7 @@ import (
_ "git.kingpenguin.tk/chteufleur/datahouse.git/routers"
"git.kingpenguin.tk/chteufleur/datahouse.git/xmpp"
"git.kingpenguin.tk/chteufleur/datahouse.git/watchlog"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"github.com/astaxie/beego"
@ -14,6 +15,8 @@ import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"time"
)
var (
@ -54,7 +57,32 @@ func main() {
if !user.IsUserExist("admin") {
user.AddUser("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")
}
/*
go xmpp_manager.Run()
go watchlog.Run()
*/
go reborn()
time.Sleep(1 * time.Second)
models.ChanRuns <- xmpp_manager.EndRun
time.Sleep(1 * time.Second)
models.ChanRuns <- watchlog.EndRun
beego.Run()
}
func reborn() {
for {
action := <- models.ChanRuns
switch action {
case xmpp_manager.EndRun:
go xmpp_manager.Run()
log.Info("XMPP Started")
case watchlog.EndRun:
go watchlog.Run()
log.Info("Watchlog Started")
}
}
}

View File

@ -4,9 +4,15 @@ import (
"github.com/gorilla/websocket"
"github.com/hpcloud/tail"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"container/list"
"time"
)
const (
EndRun = "EndRunWatchlog"
)
var (
@ -19,6 +25,8 @@ func Run() {
for line := range t.Lines {
broadcastWebSocket(line.Text)
}
time.Sleep(30 * time.Second)
models.ChanRuns <- EndRun
}

View File

@ -6,10 +6,9 @@ import (
"github.com/astaxie/beego/logs"
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/relay"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"time"
)
@ -29,6 +28,8 @@ const (
Type_unsubscribed = "unsubscribed"
Type_probe = "probe"
Type_error = "error"
EndRun = "EndRunXMPP"
)
var (
@ -42,7 +43,7 @@ var (
stream = new(xmpp.Stream)
client = new(xmpp.XMPP)
Debug = false
Debug = true
)
func init() {
@ -59,14 +60,14 @@ func must(v interface{}, err error) interface{} {
func Run() {
// Create stream and configure it as a component connection.
log.Info("XMPP Run()")
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
stream = must(xmpp.NewStream(jid.Domain+":"+serverPort, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream)
client = must(xmpp.NewClientXMPP(stream, jid, passwdStr, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
mainXMPP()
time.Sleep(5 * time.Second)
Run()
log.Debug("xmpp.Run Ended")
models.ChanRuns <- EndRun
}
@ -75,11 +76,6 @@ func mainXMPP() {
for x := range client.In {
switch v := x.(type) {
case *xmpp.Presence:
case *xmpp.Message:
log.Info("Message received : %s", v.Body)
case *xmpp.Iq:
// TODO check if the caller has privilege to ask
switch v.PayloadName().Space {
@ -94,6 +90,10 @@ func mainXMPP() {
log.Info("recv: %v", x)
}
}
// Send deconnexion
SendPresence(Status_offline, Type_unavailable, "")
log.Debug("mainXMPP Ended")
}