ui: feature support templating for index.html (#6921)

pull/6956/head
Matt Keeler 2019-12-13 14:50:07 -05:00 committed by Mike Morris
parent 1080be2087
commit 9d801d1dc2
6 changed files with 97 additions and 71 deletions

View File

@ -1,8 +1,8 @@
SHELL = bash SHELL = bash
GOGOVERSION?=$(shell grep github.com/gogo/protobuf go.mod | awk '{print $$2}') GOGOVERSION?=$(shell grep github.com/gogo/protobuf go.mod | awk '{print $$2}')
GOTOOLS = \ GOTOOLS = \
github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs \ github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs@master \
github.com/hashicorp/go-bindata/go-bindata \ github.com/hashicorp/go-bindata/go-bindata@master \
golang.org/x/tools/cmd/cover \ golang.org/x/tools/cmd/cover \
golang.org/x/tools/cmd/stringer \ golang.org/x/tools/cmd/stringer \
github.com/gogo/protobuf/protoc-gen-gofast@$(GOGOVERSION) \ github.com/gogo/protobuf/protoc-gen-gofast@$(GOGOVERSION) \

File diff suppressed because one or more lines are too long

View File

@ -162,8 +162,8 @@ func (fs *redirectFS) Open(name string) (http.File, error) {
} }
type templatedIndexFS struct { type templatedIndexFS struct {
fs http.FileSystem fs http.FileSystem
ContentPath string templateVars func() map[string]interface{}
} }
func (fs *templatedIndexFS) Open(name string) (http.File, error) { 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" { if err == nil && name == "/index.html" {
content, _ := ioutil.ReadAll(file) content, _ := ioutil.ReadAll(file)
file.Seek(0, 0) 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 var out bytes.Buffer
err = t.Execute(&out, fs) err = t.Execute(&out, fs.templateVars())
file = newTemplatedFile(&out, file) file = newTemplatedFile(&out, file)
} }
@ -317,7 +321,7 @@ func (s *HTTPServer) handler(enableDebug bool) http.Handler {
fs := assetFS() fs := assetFS()
uifs = fs 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("/robots.txt", http.FileServer(uifs))
mux.Handle(s.agent.config.UIContentPath, http.StripPrefix(s.agent.config.UIContentPath, 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 // nodeName returns the node name of the agent
func (s *HTTPServer) nodeName() string { func (s *HTTPServer) nodeName() string {
return s.agent.config.NodeName return s.agent.config.NodeName

View File

@ -42,3 +42,5 @@ func (s *HTTPServer) rewordUnknownEnterpriseFieldError(err error) error {
return err return err
} }
func (s *HTTPServer) addEnterpriseHTMLTemplateVars(vars map[string]interface{}) {}

View File

@ -105,8 +105,15 @@ module.exports = function(environment) {
} }
if (environment === 'production') { 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; return ENV;
}; };

View File

@ -23,7 +23,9 @@ module.exports = ({ appName, environment, rootURL, config }) => `
setConfig( setConfig(
'${appName}', '${appName}',
{ {
rootURL: '${rootURL}' rootURL: '${rootURL}',
CONSUL_ACLS_ENABLED: ${config.CONSUL_ACLS_ENABLED},
CONSUL_NSPACES_ENABLED: ${config.CONSUL_NSPACES_ENABLED}
} }
); );
</script> </script>