about summary refs log tree commit diff
path: root/frontend/frontend.go
blob: f1c7857ef87cd2f4f58031b0f63031f4b1aef865 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package frontend

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"encoding/json"

	"html/template"

	"github.com/gin-gonic/gin"
	"github.com/rakyll/statik/fs"

	"github.com/apex/log"

	"github.com/tweag/gerrit-queue/gerrit"
	"github.com/tweag/gerrit-queue/misc"
	_ "github.com/tweag/gerrit-queue/statik" // register static assets
	"github.com/tweag/gerrit-queue/submitqueue"
)

//loadTemplate loads a list of templates, relative to the statikFS root, and a FuncMap, and returns a template object
func loadTemplate(templateNames []string, funcMap template.FuncMap) (*template.Template, error) {
	if len(templateNames) == 0 {
		return nil, fmt.Errorf("templateNames can't be empty")
	}
	tmpl := template.New(templateNames[0]).Funcs(funcMap)
	statikFS, err := fs.New()
	if err != nil {
		return nil, err
	}

	for _, templateName := range templateNames {
		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
		}
		tmpl, err = tmpl.Parse(string(contents))
		if err != nil {
			return nil, err
		}
	}

	return tmpl, nil
}

// MakeFrontend returns a http.Handler
func MakeFrontend(rotatingLogHandler *misc.RotatingLogHandler, gerritClient *gerrit.Client, runner *submitqueue.Runner) http.Handler {
	router := gin.Default()

	projectName := gerritClient.GetProjectName()
	branchName := gerritClient.GetBranchName()

	router.GET("/", func(c *gin.Context) {
		var wipSerie *gerrit.Serie = nil
		HEAD := ""
		currentlyRunning := runner.IsCurrentlyRunning()

		// don't trigger operations requiring a lock
		if !currentlyRunning {
			wipSerie = runner.GetWIPSerie()
			HEAD = gerritClient.GetHEAD()
		}

		funcMap := template.FuncMap{
			"changesetURL": func(changeset *gerrit.Changeset) string {
				return gerritClient.GetChangesetURL(changeset)
			},
			"levelToClasses": func(level log.Level) string {
				switch level {
				case log.DebugLevel:
					return "text-muted"
				case log.InfoLevel:
					return "text-info"
				case log.WarnLevel:
					return "text-warning"
				case log.ErrorLevel:
					return "text-danger"
				case log.FatalLevel:
					return "text-danger"
				default:
					return "text-white"
				}
			},
			"fieldsToJSON": func(fields log.Fields) string {
				jsonData, _ := json.Marshal(fields)
				return string(jsonData)
			},
		}

		tmpl := template.Must(loadTemplate([]string{
			"index.tmpl.html",
			"serie.tmpl.html",
			"changeset.tmpl.html",
		}, funcMap))

		tmpl.ExecuteTemplate(c.Writer, "index.tmpl.html", gin.H{
			// Config
			"projectName": projectName,
			"branchName":  branchName,

			// State
			"currentlyRunning": currentlyRunning,
			"wipSerie":         wipSerie,
			"HEAD":             HEAD,

			// History
			"memory": rotatingLogHandler,
		})
	})
	return router
}