mirror of https://github.com/hashicorp/consul
ui: feature support templating for index.html (#6921)
parent
1080be2087
commit
9d801d1dc2
|
@ -1,8 +1,8 @@
|
|||
SHELL = bash
|
||||
GOGOVERSION?=$(shell grep github.com/gogo/protobuf go.mod | awk '{print $$2}')
|
||||
GOTOOLS = \
|
||||
github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs \
|
||||
github.com/hashicorp/go-bindata/go-bindata \
|
||||
github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs@master \
|
||||
github.com/hashicorp/go-bindata/go-bindata@master \
|
||||
golang.org/x/tools/cmd/cover \
|
||||
golang.org/x/tools/cmd/stringer \
|
||||
github.com/gogo/protobuf/protoc-gen-gofast@$(GOGOVERSION) \
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -162,8 +162,8 @@ func (fs *redirectFS) Open(name string) (http.File, error) {
|
|||
}
|
||||
|
||||
type templatedIndexFS struct {
|
||||
fs http.FileSystem
|
||||
ContentPath string
|
||||
fs http.FileSystem
|
||||
templateVars func() map[string]interface{}
|
||||
}
|
||||
|
||||
func (fs *templatedIndexFS) Open(name string) (http.File, error) {
|
||||
|
@ -171,9 +171,13 @@ func (fs *templatedIndexFS) Open(name string) (http.File, error) {
|
|||
if err == nil && name == "/index.html" {
|
||||
content, _ := ioutil.ReadAll(file)
|
||||
file.Seek(0, 0)
|
||||
t, _ := template.New("fmtedindex").Parse(string(content))
|
||||
t, err := template.New("fmtedindex").Parse(string(content))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out bytes.Buffer
|
||||
err = t.Execute(&out, fs)
|
||||
err = t.Execute(&out, fs.templateVars())
|
||||
|
||||
file = newTemplatedFile(&out, file)
|
||||
}
|
||||
|
||||
|
@ -317,7 +321,7 @@ func (s *HTTPServer) handler(enableDebug bool) http.Handler {
|
|||
fs := assetFS()
|
||||
uifs = fs
|
||||
}
|
||||
uifs = &redirectFS{fs: &templatedIndexFS{fs: uifs, ContentPath: s.agent.config.UIContentPath}}
|
||||
uifs = &redirectFS{fs: &templatedIndexFS{fs: uifs, templateVars: s.GenerateHTMLTemplateVars}}
|
||||
mux.Handle("/robots.txt", http.FileServer(uifs))
|
||||
mux.Handle(s.agent.config.UIContentPath, http.StripPrefix(s.agent.config.UIContentPath, http.FileServer(uifs)))
|
||||
}
|
||||
|
@ -335,6 +339,17 @@ func (s *HTTPServer) handler(enableDebug bool) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *HTTPServer) GenerateHTMLTemplateVars() map[string]interface{} {
|
||||
vars := map[string]interface{}{
|
||||
"ContentPath": s.agent.config.UIContentPath,
|
||||
"ACLsEnabled": s.agent.delegate.ACLsEnabled(),
|
||||
}
|
||||
|
||||
s.addEnterpriseHTMLTemplateVars(vars)
|
||||
|
||||
return vars
|
||||
}
|
||||
|
||||
// nodeName returns the node name of the agent
|
||||
func (s *HTTPServer) nodeName() string {
|
||||
return s.agent.config.NodeName
|
||||
|
|
|
@ -42,3 +42,5 @@ func (s *HTTPServer) rewordUnknownEnterpriseFieldError(err error) error {
|
|||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *HTTPServer) addEnterpriseHTMLTemplateVars(vars map[string]interface{}) {}
|
||||
|
|
|
@ -105,8 +105,15 @@ module.exports = function(environment) {
|
|||
}
|
||||
|
||||
if (environment === 'production') {
|
||||
// here you can enable a production-specific feature
|
||||
ENV = Object.assign(
|
||||
{},
|
||||
ENV,
|
||||
{
|
||||
CONSUL_ACLS_ENABLED: '{{.ACLsEnabled}}',
|
||||
CONSUL_NSPACES_ENABLED: '{{ if .NamespacesEnabled }}{{.NamespacesEnabled}}{{ else }}false{{ end }}'
|
||||
}
|
||||
);
|
||||
// here you can enable a production-specific feature
|
||||
}
|
||||
|
||||
return ENV;
|
||||
};
|
||||
|
|
|
@ -23,7 +23,9 @@ module.exports = ({ appName, environment, rootURL, config }) => `
|
|||
setConfig(
|
||||
'${appName}',
|
||||
{
|
||||
rootURL: '${rootURL}'
|
||||
rootURL: '${rootURL}',
|
||||
CONSUL_ACLS_ENABLED: ${config.CONSUL_ACLS_ENABLED},
|
||||
CONSUL_NSPACES_ENABLED: ${config.CONSUL_NSPACES_ENABLED}
|
||||
}
|
||||
);
|
||||
</script>
|
||||
|
|
Loading…
Reference in New Issue