goweb/service/template/data.go

38 lines
831 B
Go
Raw Permalink Normal View History

2018-12-06 15:18:05 +01:00
package template
import (
"net/http"
"gitlab.com/wpetit/goweb/service"
"gitlab.com/wpetit/goweb/service/build"
)
2018-12-06 15:18:05 +01:00
// Data is some data to inject into the template
type Data map[string]interface{}
// DataExtFunc is some extensions to a template's data
type DataExtFunc func(data Data) (Data, error)
// Extend returns a template's data with the given extensions
func Extend(data Data, extensions ...DataExtFunc) (Data, error) {
var err error
for _, ext := range extensions {
data, err = ext(data)
if err != nil {
return nil, err
}
}
return data, nil
}
func WithBuildInfo(w http.ResponseWriter, r *http.Request, ctn *service.Container) DataExtFunc {
return func(data Data) (Data, error) {
info, err := build.From(ctn)
if err != nil {
return nil, err
}
data["BuildInfo"] = info
return data, nil
}
}