Add BuildInfo service and template data extension
This commit is contained in:
parent
4d82e29793
commit
35bd6d65bb
|
@ -0,0 +1,7 @@
|
|||
package build
|
||||
|
||||
type Info struct {
|
||||
ProjectVersion string
|
||||
GitRef string
|
||||
BuildDate string
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package build
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName service.Name = "build"
|
||||
)
|
||||
|
||||
func ServiceProvider(projectVersion, gitRef, buildDate string) service.Provider {
|
||||
info := &Info{projectVersion, gitRef, buildDate}
|
||||
|
||||
return func(ctn *service.Container) (interface{}, error) {
|
||||
return info, nil
|
||||
}
|
||||
}
|
||||
|
||||
// From retrieves the build informations in the given service container
|
||||
func From(container *service.Container) (*Info, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := service.(*Info)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Must retrieves the user repository in the given service container or panic otherwise
|
||||
func Must(container *service.Container) *Info {
|
||||
srv, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
|
@ -1,5 +1,12 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
"gitlab.com/wpetit/goweb/service/build"
|
||||
)
|
||||
|
||||
// Data is some data to inject into the template
|
||||
type Data map[string]interface{}
|
||||
|
||||
|
@ -17,3 +24,14 @@ func Extend(data Data, extensions ...DataExtFunc) (Data, error) {
|
|||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue