Esempio n. 1
0
func TestCreateTplCtx(t *testing.T) {
	tplDir := "rootfs/templates"
	ctx := createTplCtx(tplDir, funcs)
	tpl, err := ctx.Prepare(tpl.NewFiles("index.html"))
	assert.NoErr(t, err)
	assert.NotNil(t, tpl, "returned template")
}
Esempio n. 2
0
// Index is the handler for the front page of the server
func index(s3Client *s3.Client, bucketName string, tplCtx tpl.Context) (http.Handler, error) {
	tpl, err := tplCtx.Prepare(tpl.NewFiles("index.html"))
	if err != nil {
		return nil, err
	}
	tpl = tpl.Funcs(funcMap)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		doneCh := make(chan struct{})
		defer close(doneCh)
		objCh := s3Client.ListObjects(bucketName, "", false, doneCh)
		i := 0
		for range objCh {
			i++
		}
		data := map[string]interface{}{
			"NumObjects": i,
		}
		if err := tpl.Execute(w, data); err != nil {
			http.Error(w, "error executing index template", http.StatusInternalServerError)
			return
		}
	}), nil
}