about summary refs log tree commit diff
path: root/frontend/frontend.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2019-11-21T15·12+0100
committerFlorian Klink <flokli@flokli.de>2019-11-21T15·13+0100
commit057294830e8e1915ee804d980d2b954ffbdff60a (patch)
treeb5143ce4629078d6c5c1decc540a05d3644d50fe /frontend/frontend.go
parent43f8205e85a6c5aed79041715375d199414abf0a (diff)
frontend: show submittable status and URL, add runner, revamp logging
Diffstat (limited to 'frontend/frontend.go')
-rw-r--r--frontend/frontend.go25
1 files changed, 18 insertions, 7 deletions
diff --git a/frontend/frontend.go b/frontend/frontend.go
index 240c04ae3e2d..0718bb136d1f 100644
--- a/frontend/frontend.go
+++ b/frontend/frontend.go
@@ -10,6 +10,7 @@ import (
 	"github.com/gin-gonic/gin"
 	"github.com/rakyll/statik/fs"
 
+	"github.com/tweag/gerrit-queue/gerrit"
 	_ "github.com/tweag/gerrit-queue/statik" // register static assets
 	"github.com/tweag/gerrit-queue/submitqueue"
 )
@@ -21,13 +22,13 @@ type Frontend struct {
 }
 
 //loadTemplate loads a single template from statikFS and returns a template object
-func loadTemplate(templateName string) (*template.Template, error) {
+func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) {
 	statikFS, err := fs.New()
 	if err != nil {
 		return nil, err
 	}
 
-	tmpl := template.New(templateName)
+	tmpl := template.New(templateName).Funcs(funcMap)
 	r, err := statikFS.Open("/" + templateName)
 	if err != nil {
 		return nil, err
@@ -41,9 +42,20 @@ func loadTemplate(templateName string) (*template.Template, error) {
 }
 
 // MakeFrontend configures the router and returns a new Frontend struct
-func MakeFrontend(router *gin.Engine, submitQueue *submitqueue.SubmitQueue) *Frontend {
+func MakeFrontend(runner *submitqueue.Runner, submitQueue *submitqueue.SubmitQueue) *Frontend {
+	router := gin.Default()
+
+	funcMap := template.FuncMap{
+		"isAutoSubmittable": func(serie *submitqueue.Serie) bool {
+			return submitQueue.IsAutoSubmittable(serie)
+		},
+		"changesetURL": func(changeset *gerrit.Changeset) string {
+			return submitQueue.GetChangesetURL(changeset)
+		},
+	}
+
+	tmpl := template.Must(loadTemplate("submit-queue.tmpl.html", funcMap))
 
-	tmpl := template.Must(loadTemplate("submit-queue.tmpl.html"))
 	router.SetHTMLTemplate(tmpl)
 
 	router.GET("/submit-queue.json", func(c *gin.Context) {
@@ -77,7 +89,6 @@ func MakeFrontend(router *gin.Engine, submitQueue *submitqueue.SubmitQueue) *Fro
 	}
 }
 
-// Run starts the webserver on a given address
-func (f *Frontend) Run(addr string) error {
-	return f.Router.Run(addr)
+func (f *Frontend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	f.Router.ServeHTTP(w, r)
 }