From 80d8d10c15875ba724d89b955009f78c313e5f68 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 5 May 2015 20:26:57 -0700 Subject: [PATCH] ws improvements --- drone.go | 14 ++--- server/static/scripts/services/logs.js | 2 +- server/static/scripts/services/repos.js | 2 +- server/ws.go | 72 +------------------------ 4 files changed, 8 insertions(+), 82 deletions(-) diff --git a/drone.go b/drone.go index 66e364a6..8274f891 100644 --- a/drone.go +++ b/drone.go @@ -135,17 +135,13 @@ func main() { } } - events := api.Group("/stream") + stream := api.Group("/stream") { - events.GET("/user", server.GetEvents) + stream.Use(server.SetRepo()) + stream.Use(server.SetPerm()) + stream.GET("/:owner/:name", server.GetRepoEvents) + stream.GET("/:owner/:name/:build/:number", server.GetStream) - stream := events.Group("/logs") - { - stream.Use(server.SetRepo()) - stream.Use(server.SetPerm()) - stream.GET("/:owner/:name", server.GetRepoEvents) - stream.GET("/:owner/:name/:build/:number", server.GetStream) - } } auth := r.Group("/authorize") diff --git a/server/static/scripts/services/logs.js b/server/static/scripts/services/logs.js index 177f5881..a1e26180 100644 --- a/server/static/scripts/services/logs.js +++ b/server/static/scripts/services/logs.js @@ -27,7 +27,7 @@ callback = _callback; var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'), - route = [proto, "://", $window.location.host, '/api/stream/logs/', repoName, '/', number, '/', step, '?access_token=', token].join(''); + route = [proto, "://", $window.location.host, '/api/stream/', repoName, '/', number, '/', step, '?access_token=', token].join(''); websocket = new WebSocket(route); websocket.onmessage = function (event) { diff --git a/server/static/scripts/services/repos.js b/server/static/scripts/services/repos.js index 0c9ce713..4f221660 100644 --- a/server/static/scripts/services/repos.js +++ b/server/static/scripts/services/repos.js @@ -87,7 +87,7 @@ callback = _callback; var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'), - route = [proto, "://", $window.location.host, '/api/stream/logs/'+ repo +'?access_token=', token].join(''); + route = [proto, "://", $window.location.host, '/api/stream/'+ repo +'?access_token=', token].join(''); websocket = new WebSocket(route); websocket.onmessage = function (event) { diff --git a/server/ws.go b/server/ws.go index 8657a41a..efd0d233 100644 --- a/server/ws.go +++ b/server/ws.go @@ -32,75 +32,6 @@ var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true }, } -// GetEvents will upgrade the connection to a Websocket and will stream -// event updates to the browser. -func GetEvents(c *gin.Context) { - // bus := ToBus(c) - // user := ToUser(c) - // remote := ToRemote(c) - // - // // TODO (bradrydzewski) revisit this approach at some point. - // // - // // instead of constantly checking for remote permissions, we will - // // cache them for the lifecycle of this websocket. The pro here is - // // that we are making way less external calls (good). The con is that - // // if a ton of developers conntect to websockets for long periods of - // // time with heavy build traffic (not super likely, but possible) this - // // caching strategy could take up a lot of memory. - // perms_ := map[string]*common.Perm{} - // - // // upgrade the websocket - // ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) - // if err != nil { - // c.Fail(400, err) - // return - // } - // - // ticker := time.NewTicker(pingPeriod) - // eventc := make(chan *eventbus.Event, 1) - // bus.Subscribe(eventc) - // defer func() { - // bus.Unsubscribe(eventc) - // ticker.Stop() - // ws.Close() - // close(eventc) - // }() - // - // go func() { - // for { - // select { - // case event := <-eventc: - // if event == nil { - // return // why would this ever happen? - // } - // perm, ok := perms_[event.Repo.FullName] - // if !ok { - // perm = perms(remote, user, event.Repo) - // perms_[event.Repo.FullName] = perm - // } - // - // if perm != nil && perm.Pull { - // err := ws.WriteJSON(event) - // if err != nil { - // log.Errorln(err, event) - // } - // } - // case <-ticker.C: - // ws.SetWriteDeadline(time.Now().Add(writeWait)) - // err := ws.WriteMessage(websocket.PingMessage, []byte{}) - // if err != nil { - // ws.Close() - // log.Debugf("closed websocket") - // return - // } - // } - // } - // }() - // - // readWebsocket(ws) - // log.Debugf("closed websocket") -} - // GetRepoEvents will upgrade the connection to a Websocket and will stream // event updates to the browser. func GetRepoEvents(c *gin.Context) { @@ -122,6 +53,7 @@ func GetRepoEvents(c *gin.Context) { ticker.Stop() ws.Close() close(eventc) + log.Debugf("closed websocket") }() go func() { @@ -139,7 +71,6 @@ func GetRepoEvents(c *gin.Context) { err := ws.WriteMessage(websocket.PingMessage, []byte{}) if err != nil { ws.Close() - log.Debugf("closed websocket") return } } @@ -147,7 +78,6 @@ func GetRepoEvents(c *gin.Context) { }() readWebsocket(ws) - log.Debugf("closed websocket") } func GetStream(c *gin.Context) {