diff options
author | Florian Klink <flokli@flokli.de> | 2019-11-25T17·12+0100 |
---|---|---|
committer | Florian Klink <flokli@flokli.de> | 2019-11-25T17·12+0100 |
commit | b65f56c6e2469ef23ea8bdd8534048fa382c9670 (patch) | |
tree | 3db7f297153e6a1f28919d0a751559020a4aeca6 /frontend/frontend.go | |
parent | 3898495d56f1a2f2e92bf5a9a1e88cb5a2689498 (diff) |
frontend: return state atomically, remove GetSubmitQueue
Diffstat (limited to 'frontend/frontend.go')
-rw-r--r-- | frontend/frontend.go | 51 |
1 files changed, 18 insertions, 33 deletions
diff --git a/frontend/frontend.go b/frontend/frontend.go index 387f9c756bbc..9c866aafe4da 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -16,20 +16,13 @@ import ( //TODO: log last update -// Frontend holds a gin Engine and the Sergequeue object -type Frontend struct { - Router *gin.Engine - SubmitQueue *submitqueue.SubmitQueue -} - //loadTemplate loads a single template from statikFS and returns a template object func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) { + tmpl := template.New(templateName).Funcs(funcMap) statikFS, err := fs.New() if err != nil { return nil, err } - - tmpl := template.New(templateName).Funcs(funcMap) r, err := statikFS.Open("/" + templateName) if err != nil { return nil, err @@ -43,42 +36,34 @@ func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Temp } // MakeFrontend configures the router and returns a new Frontend struct -func MakeFrontend(runner *submitqueue.Runner) *Frontend { +func MakeFrontend(runner *submitqueue.Runner) http.Handler { router := gin.Default() - submitQueue := runner.GetSubmitQueue() - - 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)) - - router.SetHTMLTemplate(tmpl) - router.GET("/submit-queue.json", func(c *gin.Context) { + submitQueue, _, _ := runner.GetState() c.JSON(http.StatusOK, submitQueue) }) router.GET("/", func(c *gin.Context) { - c.HTML(http.StatusOK, "submit-queue.tmpl.html", gin.H{ + submitQueue, _, _ := runner.GetState() + + 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.ExecuteTemplate(c.Writer, "submit-queue.tmpl.html", gin.H{ "series": submitQueue.Series, "projectName": submitQueue.ProjectName, "branchName": submitQueue.BranchName, "HEAD": submitQueue.HEAD, }) }) - return &Frontend{ - Router: router, - SubmitQueue: submitQueue, - } -} - -func (f *Frontend) ServeHTTP(w http.ResponseWriter, r *http.Request) { - f.Router.ServeHTTP(w, r) + return router } |