tvl-depot/third_party/gerrit-queue/frontend/frontend.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
2.8 KiB
Go
Raw Normal View History

2019-11-18 15:40:46 +01:00
package frontend
import (
"fmt"
"io/ioutil"
2019-11-18 15:40:46 +01:00
"net/http"
2019-12-04 15:21:17 +01:00
"encoding/json"
2019-11-18 15:40:46 +01:00
"html/template"
"github.com/rakyll/statik/fs"
2019-12-04 15:21:17 +01:00
"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
2019-11-18 15:40:46 +01:00
"github.com/tweag/gerrit-queue/submitqueue"
)
2019-11-25 17:43:56 +01:00
//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 {
projectName := gerritClient.GetProjectName()
branchName := gerritClient.GetBranchName()
2019-11-18 15:40:46 +01:00
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
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)
},
2019-12-04 15:21:17 +01:00
"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)
},
}
2019-11-27 11:38:54 +01:00
tmpl := template.Must(loadTemplate([]string{
"index.tmpl.html",
2019-11-27 11:38:54 +01:00
"serie.tmpl.html",
"changeset.tmpl.html",
}, funcMap))
tmpl.ExecuteTemplate(w, "index.tmpl.html", map[string]interface{}{
// Config
"projectName": projectName,
"branchName": branchName,
// State
"currentlyRunning": currentlyRunning,
"wipSerie": wipSerie,
"HEAD": HEAD,
// History
"memory": rotatingLogHandler,
2019-11-18 15:40:46 +01:00
})
})
return mux
2019-11-18 15:40:46 +01:00
}