From 3b7c9738a3f545589ea228c17260cc398b79fb45 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 25 Apr 2015 16:16:56 -0700 Subject: [PATCH 1/4] ability to squash and embed static files --- .gitignore | 6 +++++- Makefile | 17 ++++++++++++++++- drone.go | 22 ++++++++++++++++++---- queue/plugin/server.go | 2 +- server/static/scripts/term.js | 2 +- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 8383f7998..bff14d2cf 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,9 @@ drone.sublime-workspace *.db *.txt *.toml +bindata.go -drone \ No newline at end of file +# combined assets +server/static/drone.js + +drone diff --git a/Makefile b/Makefile index 7a9c3bd0b..986d76600 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ SHA := $(shell git rev-parse --short HEAD) VERSION := 0.4.0-alpha -all: build +all: concat bindata build deps: + go get -u github.com/jteeuwen/go-bindata/... go get -t -v ./... test: @@ -16,3 +17,17 @@ build: clean: find . -name "*.out" -delete rm -f drone + rm -f bindata.go + +concat: + cat server/static/scripts/drone.js \ + server/static/scripts/services/*.js \ + server/static/scripts/filters/*.js \ + server/static/scripts/controllers/*.js \ + server/static/scripts/term.js > server/static/drone.js + +bindata_debug: + go-bindata --debug server/static/... + +bindata: + go-bindata server/static/... \ No newline at end of file diff --git a/drone.go b/drone.go index 7083c5ea9..50cba8c7a 100644 --- a/drone.go +++ b/drone.go @@ -2,6 +2,7 @@ package main import ( "flag" + "net/http" "github.com/gin-gonic/gin" @@ -11,16 +12,17 @@ import ( "github.com/drone/drone/server" "github.com/drone/drone/server/session" "github.com/drone/drone/settings" + "github.com/elazarl/go-bindata-assetfs" queue "github.com/drone/drone/queue/builtin" ) -var path = flag.String("config", "drone.toml", "") +var conf = flag.String("config", "drone.toml", "") func main() { flag.Parse() - settings, err := settings.Parse(*path) + settings, err := settings.Parse(*conf) if err != nil { panic(err) } @@ -134,6 +136,18 @@ func main() { r.NoRoute(func(c *gin.Context) { c.File("server/static/index.html") }) - r.Static("/static", "server/static") - r.Run(settings.Server.Addr) + + http.Handle("/static/", static()) + http.Handle("/", r) + http.ListenAndServe(settings.Server.Addr, nil) +} + +// static is a helper function that will setup handlers +// for serving static files. +func static() http.Handler { + return http.StripPrefix("/static/", http.FileServer(&assetfs.AssetFS{ + Asset: Asset, + AssetDir: AssetDir, + Prefix: "server/static", + })) } diff --git a/queue/plugin/server.go b/queue/plugin/server.go index baf355072..bab3b931c 100644 --- a/queue/plugin/server.go +++ b/queue/plugin/server.go @@ -72,7 +72,7 @@ func remove(c *gin.Context) { func pull(c *gin.Context) { q := fromContext(c) var work *queue.Work - if c.Request.FormValue("ack") != "" { + if c.Request.FormValue("ack") == "true" { work = q.PullAck() } else { work = q.Pull() diff --git a/server/static/scripts/term.js b/server/static/scripts/term.js index 925e463a1..0e925abad 100644 --- a/server/static/scripts/term.js +++ b/server/static/scripts/term.js @@ -387,4 +387,4 @@ Filter = (function() { return Filter; -})(); \ No newline at end of file +})(); From 712c18459f2ded422723337f98a06e47c8234bfc Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 25 Apr 2015 16:43:51 -0700 Subject: [PATCH 2/4] serving the index.html page from bindata --- drone.go | 12 +++++++++++- server/server.go | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/drone.go b/drone.go index 50cba8c7a..cca4dc82b 100644 --- a/drone.go +++ b/drone.go @@ -2,6 +2,7 @@ package main import ( "flag" + "html/template" "net/http" "github.com/gin-gonic/gin" @@ -133,8 +134,9 @@ func main() { auth.POST("", server.GetLogin) } + r.SetHTMLTemplate(index()) r.NoRoute(func(c *gin.Context) { - c.File("server/static/index.html") + c.HTML(200, "index.html", nil) }) http.Handle("/static/", static()) @@ -151,3 +153,11 @@ func static() http.Handler { Prefix: "server/static", })) } + +// index is a helper function that will setup a template +// for rendering the main angular index.html file. +func index() *template.Template { + file := MustAsset("server/static/index.html") + filestr := string(file) + return template.Must(template.New("index.html").Parse(filestr)) +} diff --git a/server/server.go b/server/server.go index 55ef51d2f..ce96d4c43 100644 --- a/server/server.go +++ b/server/server.go @@ -164,10 +164,10 @@ func SetRepo() gin.HandlerFunc { r, err := ds.Repo(owner + "/" + name) switch { case err != nil && u != nil: - c.Fail(401, err) + c.Fail(404, err) return case err != nil && u == nil: - c.Fail(404, err) + c.Fail(401, err) return } c.Set("repo", r) From 8e6fbc8cebe7e357940505bc6fe7f79613fee242 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 25 Apr 2015 16:46:02 -0700 Subject: [PATCH 3/4] fixed bindata build issues --- .drone.yml | 5 +++-- Makefile | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index 65b5070d1..16b4bd9f3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -5,8 +5,9 @@ env: - GOROOT=/usr/local/go - PATH=$PATH:$GOROOT/bin:$GOPATH/bin script: - - make deps - - make + - go get -u github.com/jteeuwen/go-bindata/... + - make bindata deps + - make build - make test notify: diff --git a/Makefile b/Makefile index 986d76600..79e879570 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,6 @@ VERSION := 0.4.0-alpha all: concat bindata build deps: - go get -u github.com/jteeuwen/go-bindata/... go get -t -v ./... test: From 96b49cf0fcb22c4bb542406fc0deadf36bcce704 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 25 Apr 2015 21:27:24 -0700 Subject: [PATCH 4/4] website subscribes to build events, update pages --- .gitignore | 4 +- Makefile | 2 +- drone.go | 12 ++++-- server/queue.go | 8 ++-- server/static/scripts/controllers/builds.js | 43 ++++++++++++++------- server/static/scripts/views/build.html | 2 +- server/static/scripts/views/builds.html | 2 +- 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index bff14d2cf..df07484a4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,9 +13,7 @@ drone.sublime-workspace *.db *.txt *.toml +*.min.js bindata.go -# combined assets -server/static/drone.js - drone diff --git a/Makefile b/Makefile index 79e879570..073067ced 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ concat: server/static/scripts/services/*.js \ server/static/scripts/filters/*.js \ server/static/scripts/controllers/*.js \ - server/static/scripts/term.js > server/static/drone.js + server/static/scripts/term.js > server/static/scripts/drone.min.js bindata_debug: go-bindata --debug server/static/... diff --git a/drone.go b/drone.go index cca4dc82b..c4fc0ffd5 100644 --- a/drone.go +++ b/drone.go @@ -109,12 +109,16 @@ func main() { queue := api.Group("/queue") { - queue.Use(server.SetRepo()) queue.GET("", server.GetQueue) queue.POST("/pull", server.PollBuild) - queue.POST("/push/:owner/:name", server.PushBuild) - queue.POST("/push/:owner/:name/:build", server.PushTask) - queue.POST("/push/:owner/:name/:build/:task/logs", server.PushLogs) + + push := queue.Group("/push/:owner/:name") + { + push.Use(server.SetRepo()) + push.POST("", server.PushBuild) + push.POST("/:build", server.PushTask) + push.POST("/:build/:task/logs", server.PushLogs) + } } events := api.Group("/stream") diff --git a/server/queue.go b/server/queue.go index e23479a22..674bdd6b5 100644 --- a/server/queue.go +++ b/server/queue.go @@ -48,9 +48,11 @@ func PushBuild(c *gin.Context) { return } - if repo.Last == nil || build.Number >= repo.Last.Number { - repo.Last = build - store.SetRepo(repo) + if build.State != common.StatePending && build.State != common.StateRunning { + if repo.Last == nil || build.Number >= repo.Last.Number { + repo.Last = build + store.SetRepo(repo) + } } // <-- FIXME diff --git a/server/static/scripts/controllers/builds.js b/server/static/scripts/controllers/builds.js index c504c7340..248a490ad 100644 --- a/server/static/scripts/controllers/builds.js +++ b/server/static/scripts/controllers/builds.js @@ -47,17 +47,28 @@ } // update repository $scope.repo = event.repo; - $scope.apply(); - - if (event.build.number !== parseInt(number)) { - return; // ignore - } - // update the build status - $scope.build.state = event.build.state; - $scope.build.started = event.build.started; - $scope.build.finished = event.build.finished; - $scope.build.duration = event.build.duration; $scope.$apply(); + + var added = false; + for (var i=0;i<$scope.builds.length;i++) { + var build = $scope.builds[i]; + if (event.build.number !== build.number) { + continue; // ignore + } + // update the build status + build.state = event.build.state; + build.started_at = event.build.started_at; + build.finished_at = event.build.finished_at; + build.duration = event.build.duration; + $scope.builds[i] = build; + $scope.$apply(); + added = true; + } + + if (!added) { + $scope.builds.push(event.build); + $scope.$apply(); + } }); } @@ -97,7 +108,9 @@ // fetch the logs for the finished build. logs.get(fullName, number, step).then(function(payload){ - $scope.logs = payload.data; + var convert = new Filter({stream:false,newline:false}); + var term = document.getElementById("term") + term.innerHTML = convert.toHtml(payload.data); }).catch(function(err){ $scope.error = err; }); @@ -127,8 +140,8 @@ } // update the build status $scope.build.state = event.build.state; - $scope.build.started = event.build.started; - $scope.build.finished = event.build.finished; + $scope.build.started_at = event.build.started_at; + $scope.build.finished_at = event.build.finished_at; $scope.build.duration = event.build.duration; $scope.$apply(); @@ -137,8 +150,8 @@ } // update the task status $scope.task.state = event.task.state; - $scope.task.started = event.task.started; - $scope.task.finished = event.task.finished; + $scope.task.started_at = event.task.started_at; + $scope.task.finished_at = event.task.finished_at; $scope.task.duration = event.task.duration; $scope.task.exit_code = event.task.exit_code; $scope.$apply(); diff --git a/server/static/scripts/views/build.html b/server/static/scripts/views/build.html index b022b7e99..85668d05d 100644 --- a/server/static/scripts/views/build.html +++ b/server/static/scripts/views/build.html @@ -57,7 +57,7 @@
-
{{ logs }}
+
{{ logs }}
diff --git a/server/static/scripts/views/builds.html b/server/static/scripts/views/builds.html index 7fb5e6d98..984d126b7 100644 --- a/server/static/scripts/views/builds.html +++ b/server/static/scripts/views/builds.html @@ -21,7 +21,7 @@ - +
{{ build.number }} {{ build.state }} {{ build.started_at | fromNow }}