use statik to bundle assets, add nix-shell/lorri .envrc
This commit is contained in:
parent
987b539e33
commit
dbd7b4807a
8 changed files with 67 additions and 3 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue