about summary refs log tree commit diff
path: root/frontend/frontend.go
blob: 8cd3d9a9b0928bb24ea45194cceebb63d93f3a61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)
}