about summary refs log tree commit diff
path: root/frontend/frontend.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2019-11-18T14·40+0100
committerFlorian Klink <flokli@flokli.de>2019-11-18T14·40+0100
commit987b539e335ebc93eba9549ebf8204d612b7a148 (patch)
treed41adf014d25d05be5d46b6935d168e0b9bbbf16 /frontend/frontend.go
initial import
Diffstat (limited to 'frontend/frontend.go')
-rw-r--r--frontend/frontend.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/frontend/frontend.go b/frontend/frontend.go
new file mode 100644
index 000000000000..8cd3d9a9b092
--- /dev/null
+++ b/frontend/frontend.go
@@ -0,0 +1,53 @@
+package frontend
+
+import (
+	"fmt"
+	"net/http"
+
+	"github.com/gin-gonic/gin"
+	"github.com/tweag/gerrit-queue/submitqueue"
+)
+
+// Frontend holds a gin Engine and the Sergequeue object
+type Frontend struct {
+	Router      *gin.Engine
+	SubmitQueue *submitqueue.SubmitQueue
+}
+
+// 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/*")
+	router.GET("/submit-queue.json", func(c *gin.Context) {
+		// FIXME: do this periodically
+		err := submitQueue.UpdateHEAD()
+		if err != nil {
+			c.AbortWithError(http.StatusBadGateway, fmt.Errorf("unable to update HEAD"))
+		}
+		c.JSON(http.StatusOK, submitQueue)
+	})
+
+	router.GET("/", func(c *gin.Context) {
+		// FIXME: do this periodically
+		// TODO: add hyperlinks to changesets
+		err := submitQueue.UpdateHEAD()
+		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,
+			"branchName":  submitQueue.BranchName,
+			"HEAD":        submitQueue.HEAD,
+		})
+	})
+	return &Frontend{
+		Router:      router,
+		SubmitQueue: submitQueue,
+	}
+}
+
+// Run starts the webserver on a given address
+func (f *Frontend) Run(addr string) error {
+	return f.Router.Run(addr)
+}