From dbd7b4807a6364630d43edf309eb4b43f4bb3658 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 18 Nov 2019 16:29:57 +0100 Subject: use statik to bundle assets, add nix-shell/lorri .envrc --- .envrc | 13 +++++++++++++ .gitignore | 1 + frontend/frontend.go | 34 ++++++++++++++++++++++++++++++++-- go.mod | 3 ++- go.sum | 6 ++++++ main.go | 2 ++ public/submit-queue.tmpl.html | 38 ++++++++++++++++++++++++++++++++++++++ shell.nix | 11 +++++++++++ templates/submit-queue.tmpl.html | 38 -------------------------------------- 9 files changed, 105 insertions(+), 41 deletions(-) create mode 100644 .envrc create mode 100644 public/submit-queue.tmpl.html create mode 100644 shell.nix delete mode 100644 templates/submit-queue.tmpl.html diff --git a/.envrc b/.envrc new file mode 100644 index 000000000000..211e0d451c4e --- /dev/null +++ b/.envrc @@ -0,0 +1,13 @@ +# This configures [direnv](https://direnv.net/) if installed and enabled to +# automatically enter the nix-shell defined in `shell.nix`, +# either by using lorri if available, or nix-shell otherwise. + +if type lorri &>/dev/null; then + eval "$(lorri direnv)" +else + # fall back to using direnv's builtin nix support (blocking) + use nix +fi + +# Load private overrides +[[ -f .envrc.private ]] && source_env .envrc.private diff --git a/.gitignore b/.gitignore index 598ae7928baa..347fbd0aa364 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /.vscode +/statik diff --git a/frontend/frontend.go b/frontend/frontend.go index 8cd3d9a9b092..240c04ae3e2d 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -2,9 +2,15 @@ package frontend import ( "fmt" + "io/ioutil" "net/http" + "html/template" + "github.com/gin-gonic/gin" + "github.com/rakyll/statik/fs" + + _ "github.com/tweag/gerrit-queue/statik" // register static assets "github.com/tweag/gerrit-queue/submitqueue" ) @@ -14,11 +20,34 @@ type Frontend struct { SubmitQueue *submitqueue.SubmitQueue } +//loadTemplate loads a single template from statikFS and returns a template object +func loadTemplate(templateName string) (*template.Template, error) { + statikFS, err := fs.New() + if err != nil { + return nil, err + } + + tmpl := template.New(templateName) + r, err := statikFS.Open("/" + templateName) + if err != nil { + return nil, err + } + defer r.Close() + contents, err := ioutil.ReadAll(r) + if err != nil { + return nil, err + } + return tmpl.Parse(string(contents)) +} + // MakeFrontend configures the router and returns a new Frontend struct func MakeFrontend(router *gin.Engine, submitQueue *submitqueue.SubmitQueue) *Frontend { - // FIXME: use go generators and statik - router.LoadHTMLGlob("templates/*") + + tmpl := template.Must(loadTemplate("submit-queue.tmpl.html")) + router.SetHTMLTemplate(tmpl) + router.GET("/submit-queue.json", func(c *gin.Context) { + // FIXME: do this periodically err := submitQueue.UpdateHEAD() if err != nil { @@ -34,6 +63,7 @@ func MakeFrontend(router *gin.Engine, submitQueue *submitqueue.SubmitQueue) *Fro if err != nil { c.AbortWithError(http.StatusBadGateway, fmt.Errorf("unable to update HEAD")) } + c.HTML(http.StatusOK, "submit-queue.tmpl.html", gin.H{ "series": submitQueue.Series, "projectName": submitQueue.ProjectName, diff --git a/go.mod b/go.mod index 20d2e152cc9f..c428ff3cb44f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/andygrunwald/go-gerrit v0.0.0-20190825170856-5959a9bf9ff8 github.com/gin-gonic/gin v1.4.0 github.com/google/go-querystring v1.0.0 // indirect + github.com/rakyll/statik v0.1.6 github.com/sirupsen/logrus v1.4.2 - github.com/urfave/cli v1.22.1 // indirect + github.com/urfave/cli v1.22.1 ) diff --git a/go.sum b/go.sum index 000f431ed4fc..b5f1dd9f9cc1 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/andygrunwald/go-gerrit v0.0.0-20190825170856-5959a9bf9ff8/go.mod h1:0 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= @@ -19,7 +20,10 @@ github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs= +github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= @@ -29,12 +33,14 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/main.go b/main.go index 0b92a913ae59..9bd8cd1feab1 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,5 @@ +//go:generate statik -f + package main import ( diff --git a/public/submit-queue.tmpl.html b/public/submit-queue.tmpl.html new file mode 100644 index 000000000000..f1320a0c2dde --- /dev/null +++ b/public/submit-queue.tmpl.html @@ -0,0 +1,38 @@ + + + + Gerrit Submit Queue + + + + +

Gerrit Submit Queue

+

{{ .projectName }}/{{ .branchName }} is at {{ printf "%.7s" .HEAD }}

+

Current Queue:

+ {{ range $serie := .series }} +
+ {{ range $changeset := $serie.ChangeSets}} +
+
+
{{ $changeset.Subject }}
+ #{{ $changeset.Number }} +
+
+ {{ $changeset.OwnerName }} + + {{ if $changeset.IsVerified }}+1 (CI){{ end }} + {{ if $changeset.IsCodeReviewed }}+2 (CR){{ end }} + +
+
+ + {{ $changeset.CommitID }} + +
+
+ {{ end }} +
+
+ {{ end }} + + diff --git a/shell.nix b/shell.nix new file mode 100644 index 000000000000..b28839aa28c8 --- /dev/null +++ b/shell.nix @@ -0,0 +1,11 @@ +let + pkgs = (import (builtins.fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/5de728659b412bcf7d18316a4b71d9a6e447f460.tar.gz"; + sha256 = "1bdykda8k8gl2vcp36g27xf3437ig098yrhjp0hclv7sn6dp2w1l"; + })) {}; +in + pkgs.mkShell { + buildInputs = [ + pkgs.statik + ]; + } diff --git a/templates/submit-queue.tmpl.html b/templates/submit-queue.tmpl.html deleted file mode 100644 index f1320a0c2dde..000000000000 --- a/templates/submit-queue.tmpl.html +++ /dev/null @@ -1,38 +0,0 @@ - - - - Gerrit Submit Queue - - - - -

Gerrit Submit Queue

-

{{ .projectName }}/{{ .branchName }} is at {{ printf "%.7s" .HEAD }}

-

Current Queue:

- {{ range $serie := .series }} -
- {{ range $changeset := $serie.ChangeSets}} -
-
-
{{ $changeset.Subject }}
- #{{ $changeset.Number }} -
-
- {{ $changeset.OwnerName }} - - {{ if $changeset.IsVerified }}+1 (CI){{ end }} - {{ if $changeset.IsCodeReviewed }}+2 (CR){{ end }} - -
-
- - {{ $changeset.CommitID }} - -
-
- {{ end }} -
-
- {{ end }} - - -- cgit 1.4.1