re-added code to read/write websocket data. added to amber templates

This commit is contained in:
Brad 2014-06-08 23:59:23 -07:00
parent b0255af7a9
commit 4d4defc416
11 changed files with 64 additions and 41 deletions

View file

@ -3,6 +3,7 @@ package handler
import ( import (
"net/http" "net/http"
"github.com/drone/drone/server/channel"
"github.com/drone/drone/server/render" "github.com/drone/drone/server/render"
"github.com/drone/drone/server/resource/commit" "github.com/drone/drone/server/resource/commit"
"github.com/drone/drone/server/resource/perm" "github.com/drone/drone/server/resource/perm"
@ -94,12 +95,17 @@ func (s *SiteHandler) GetRepo(w http.ResponseWriter, r *http.Request) error {
User *user.User User *user.User
Repo *repo.Repo Repo *repo.Repo
Branch string Branch string
Token string Channel string
Stream string
Branches []*commit.Commit Branches []*commit.Commit
Commits []*commit.Commit Commits []*commit.Commit
Commit *commit.Commit Commit *commit.Commit
}{User: usr, Repo: arepo} }{User: usr, Repo: arepo}
// generate a token for connecting to the streaming server
// to get notified of feed items.
data.Channel = channel.Create(host + "/" + owner + "/" + name + "/")
// if commit details are provided we should retrieve the build details // if commit details are provided we should retrieve the build details
// and serve the build page. // and serve the build page.
if len(sha) != 0 { if len(sha) != 0 {
@ -107,6 +113,11 @@ func (s *SiteHandler) GetRepo(w http.ResponseWriter, r *http.Request) error {
if err != nil { if err != nil {
return s.render(w, "404.html", nil) return s.render(w, "404.html", nil)
} }
// generate a token for connecting to the streaming server
// to get notified of feed items.
data.Stream = channel.Create(host + "/" + owner + "/" + name + "/" + branch + "/" + sha)
return s.render(w, "repo_commit.html", &data) return s.render(w, "repo_commit.html", &data)
} }

View file

@ -6,6 +6,8 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"code.google.com/p/go.net/websocket"
"github.com/drone/drone/server/channel"
"github.com/drone/drone/server/database" "github.com/drone/drone/server/database"
"github.com/drone/drone/server/handler" "github.com/drone/drone/server/handler"
"github.com/drone/drone/server/render" "github.com/drone/drone/server/render"
@ -93,6 +95,9 @@ func main() {
// TODO we need to replace this with go.rice // TODO we need to replace this with go.rice
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// server websocket data
http.Handle("/feed", websocket.Handler(channel.Read))
// register the router // register the router
// TODO we disabled nosurf because it was impacting API calls. // TODO we disabled nosurf because it was impacting API calls.
// we need to disable nosurf for api calls (ie not coming from website). // we need to disable nosurf for api calls (ie not coming from website).

View file

@ -85,15 +85,15 @@ func (w *worker) execute(task *BuildTask) error {
// make sure a channel exists for the repository, // make sure a channel exists for the repository,
// the commit, and the commit output (TODO) // the commit, and the commit output (TODO)
reposlug := fmt.Sprintf("%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name) reposlug := fmt.Sprintf("%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name)
commitslug := fmt.Sprintf("%s/%s/%s/commit/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Sha) //commitslug := fmt.Sprintf("%s/%s/%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Sha)
consoleslug := fmt.Sprintf("%s/%s/%s/commit/%s/%s/console", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Sha) consoleslug := fmt.Sprintf("%s/%s/%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Sha)
channel.Create(reposlug) channel.Create(reposlug)
channel.Create(commitslug) //channel.Create(commitslug)
channel.CreateStream(consoleslug) channel.CreateStream(consoleslug)
// notify the channels that the commit and build started // notify the channels that the commit and build started
channel.SendJSON(reposlug, task.Commit) channel.SendJSON(reposlug, task.Commit)
channel.SendJSON(commitslug, task.Commit) //channel.SendJSON(commitslug, task.Commit)
var buf = &bufferWrapper{channel: consoleslug} var buf = &bufferWrapper{channel: consoleslug}
@ -145,7 +145,7 @@ func (w *worker) execute(task *BuildTask) error {
// notify the channels that the commit and build finished // notify the channels that the commit and build finished
channel.SendJSON(reposlug, task.Commit) channel.SendJSON(reposlug, task.Commit)
channel.SendJSON(commitslug, task.Commit) //channel.SendJSON(commitslug, task.Commit)
channel.Close(consoleslug) channel.Close(consoleslug)
// send all "finished" notifications // send all "finished" notifications

View file

@ -32,10 +32,10 @@ func NewSession(users user.UserManager) Session {
// User gets the currently authenticated user from the secure cookie session. // User gets the currently authenticated user from the secure cookie session.
func (s *session) User(r *http.Request) *user.User { func (s *session) User(r *http.Request) *user.User {
if true { //if true {
user, _ := s.users.Find(1) // user, _ := s.users.Find(1)
return user // return user
} //}
switch { switch {
case r.FormValue("access_token") == "": case r.FormValue("access_token") == "":

View file

@ -76,7 +76,6 @@ if(typeof(Drone) === 'undefined') { Drone = {}; }
onClose: function(e) { onClose: function(e) {
console.log('output websocket closed: ' + JSON.stringify(e)); console.log('output websocket closed: ' + JSON.stringify(e));
window.location.reload();
} }
}; };

View file

@ -73,5 +73,11 @@
$(".timeago").timeago(); $(".timeago").timeago();
}); });
</script> </script>
<script>
var ws = new WebSocket((window.location.protocol=='http:'?'ws':'wss')+'://'+window.location.host+'/feed?token='+{{.Channel}});
ws.onmessage = function (e) {
console.log(e);
};
</script>
</body> </body>
</html> </html>

View file

@ -80,23 +80,18 @@
$(".timeago").timeago(); $(".timeago").timeago();
}); });
</script>{{$__amber_38 := .Build.Status}}{{$__amber_39 := __amber_eql $__amber_38 "Pending"}}{{$__amber_40 := .Build.Status}}{{$__amber_41 := __amber_eql $__amber_40 "Started"}}{{$__amber_42 := or $__amber_41 $__amber_39}}{{if $__amber_42}} </script>{{$__amber_38 := .Build.Status}}{{$__amber_39 := __amber_eql $__amber_38 "Pending"}}{{$__amber_40 := .Build.Status}}{{$__amber_41 := __amber_eql $__amber_40 "Started"}}{{$__amber_42 := or $__amber_41 $__amber_39}}{{if $__amber_42}}
<script>
var ws = new WebSocket((window.location.protocol=='http:'?'ws':'wss')+'://'+window.location.host+'/feed?token={{.Channel}}');
ws.onmessage = function (e) {
console.log(e);
};
</script>
<script> <script>
$(document).ready(function() { $(document).ready(function() {
var commitUpdates = new Drone.CommitUpdates('/feed?token='+ {{.Token}}); var commitUpdates = new Drone.CommitUpdates('/feed?token={{.Stream}}');
var outputBox = document.getElementById('stdout'); var outputBox = document.getElementById('stdout');
commitUpdates.autoFollow = true;
commitUpdates.startOutput(outputBox); commitUpdates.startOutput(outputBox);
$("#follow").on("click", function(e) {
e.preventDefault();
if(commitUpdates.autoFollow) {
commitUpdates.autoFollow = false;
$(this).text("Follow");
} else {
commitUpdates.autoFollow = true;
$(this).text("Stop following");
}
});
}); });
</script>{{else}} </script>{{else}}
<script> <script>

View file

@ -82,9 +82,13 @@
$(document).ready(function() { $(document).ready(function() {
$(".timeago").timeago(); $(".timeago").timeago();
}); });
</script>{{$__amber_50 := .Repo.Active}}{{if $__amber_50}} </script>{{$__amber_50 := .Repo.Active}}{{if $__amber_50}}
<script></script>{{else}} <script>
var ws = new WebSocket((window.location.protocol=='http:'?'ws':'wss')+'://'+window.location.host+'/feed?token={{.Channel}}');
ws.onmessage = function (e) {
console.log(e);
};
</script>{{else}}
<script> <script>
document.forms[0].onsubmit = function(event) { document.forms[0].onsubmit = function(event) {
var form = event.target; var form = event.target;

View file

@ -45,4 +45,10 @@ block append scripts
script script
$(document).ready(function() { $(document).ready(function() {
$(".timeago").timeago(); $(".timeago").timeago();
}); });
script
var ws = new WebSocket((window.location.protocol=='http:'?'ws':'wss')+'://'+window.location.host+'/feed?token='+#{Channel});
ws.onmessage = function (e) {
console.log(e);
};

View file

@ -51,23 +51,17 @@ block append scripts
}); });
if Build.Status == "Started" || Build.Status == "Pending" if Build.Status == "Started" || Build.Status == "Pending"
script
var ws = new WebSocket((window.location.protocol=='http:'?'ws':'wss')+'://'+window.location.host+'/feed?token=#{Channel}');
ws.onmessage = function (e) {
console.log(e);
};
script script
$(document).ready(function() { $(document).ready(function() {
var commitUpdates = new Drone.CommitUpdates('/feed?token='+ #{Token}); var commitUpdates = new Drone.CommitUpdates('/feed?token=#{Stream}');
var outputBox = document.getElementById('stdout'); var outputBox = document.getElementById('stdout');
commitUpdates.autoFollow = true;
commitUpdates.startOutput(outputBox); commitUpdates.startOutput(outputBox);
$("#follow").on("click", function(e) {
e.preventDefault();
if(commitUpdates.autoFollow) {
commitUpdates.autoFollow = false;
$(this).text("Follow");
} else {
commitUpdates.autoFollow = true;
$(this).text("Stop following");
}
});
}); });
else else
script script

View file

@ -58,9 +58,12 @@ block append scripts
$(".timeago").timeago(); $(".timeago").timeago();
}); });
if Repo.Active if Repo.Active
script script
var ws = new WebSocket((window.location.protocol=='http:'?'ws':'wss')+'://'+window.location.host+'/feed?token=#{Channel}');
ws.onmessage = function (e) {
console.log(e);
};
else else
script script
document.forms[0].onsubmit = function(event) { document.forms[0].onsubmit = function(event) {