about summary refs log tree commit diff
path: root/frontend
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2019-11-18T15·29+0100
committerFlorian Klink <flokli@flokli.de>2019-11-18T15·29+0100
commitdbd7b4807a6364630d43edf309eb4b43f4bb3658 (patch)
tree6150837d72f22232bdf123acd55edc34b9c1f47c /frontend
parent987b539e335ebc93eba9549ebf8204d612b7a148 (diff)
use statik to bundle assets, add nix-shell/lorri .envrc
Diffstat (limited to 'frontend')
-rw-r--r--frontend/frontend.go34
1 files changed, 32 insertions, 2 deletions
diff --git a/frontend/frontend.go b/frontend/frontend.go
index 8cd3d9a9b092..240c04ae3e2d 100644
--- a/frontend/frontend.go
+++ b/frontend/frontend.go
@@ -2,9 +2,15 @@ package frontend
 
 import (
 	"fmt"
+	"io/ioutil"
 	"net/http"
 
+	"html/template"
+
 	"github.com/gin-gonic/gin"
+	"github.com/rakyll/statik/fs"
+
+	_ "github.com/tweag/gerrit-queue/statik" // register static assets
 	"github.com/tweag/gerrit-queue/submitqueue"
 )
 
@@ -14,11 +20,34 @@ type Frontend struct {
 	SubmitQueue *submitqueue.SubmitQueue
 }
 
+//loadTemplate loads a single template from statikFS and returns a template object
+func loadTemplate(templateName string) (*template.Template, error) {
+	statikFS, err := fs.New()
+	if err != nil {
+		return nil, err
+	}
+
+	tmpl := template.New(templateName)
+	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
+	}
+	return tmpl.Parse(string(contents))
+}
+
 // 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/*")
+
+	tmpl := template.Must(loadTemplate("submit-queue.tmpl.html"))
+	router.SetHTMLTemplate(tmpl)
+
 	router.GET("/submit-queue.json", func(c *gin.Context) {
+
 		// FIXME: do this periodically
 		err := submitQueue.UpdateHEAD()
 		if err != nil {
@@ -34,6 +63,7 @@ func MakeFrontend(router *gin.Engine, submitQueue *submitqueue.SubmitQueue) *Fro
 		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,