Skip to content

Commit 3b47317

Browse files
committed
Serve extension scripts and stylesheets as URLs
1 parent 65ce1cf commit 3b47317

File tree

4 files changed

+47
-237
lines changed

4 files changed

+47
-237
lines changed

pkg/assets/apiserver/asset_apiserver.go

+1-43
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ func (c completedAssetServerConfig) New(delegationTarget genericapiserver.Delega
129129
if err := c.addAssets(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
130130
return nil, err
131131
}
132-
if err := c.addExtensionScripts(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
133-
return nil, err
134-
}
135-
if err := c.addExtensionStyleSheets(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
136-
return nil, err
137-
}
138-
c.addExtensionFiles(s.GenericAPIServer.Handler.NonGoRestfulMux)
139132
if err := c.addWebConsoleConfig(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
140133
return nil, err
141134
}
@@ -173,41 +166,6 @@ func (c completedAssetServerConfig) addAssets(serverMux *genericmux.PathRecorder
173166
return nil
174167
}
175168

176-
func (c completedAssetServerConfig) addExtensionScripts(serverMux *genericmux.PathRecorderMux) error {
177-
// Extension scripts
178-
extScriptsPath := path.Join(c.PublicURL.Path, "scripts/extensions.js")
179-
extScriptsHandler, err := assets.ExtensionScriptsHandler(c.Options.ExtensionScripts, c.Options.ExtensionDevelopment)
180-
if err != nil {
181-
return err
182-
}
183-
extScriptsHandler = assets.SecurityHeadersHandler(extScriptsHandler)
184-
serverMux.UnlistedHandle(extScriptsPath, assets.GzipHandler(extScriptsHandler))
185-
return nil
186-
}
187-
188-
func (c completedAssetServerConfig) addExtensionStyleSheets(serverMux *genericmux.PathRecorderMux) error {
189-
// Extension stylesheets
190-
extStylesheetsPath := path.Join(c.PublicURL.Path, "styles/extensions.css")
191-
extStylesheetsHandler, err := assets.ExtensionStylesheetsHandler(c.Options.ExtensionStylesheets, c.Options.ExtensionDevelopment)
192-
if err != nil {
193-
return err
194-
}
195-
extStylesheetsHandler = assets.SecurityHeadersHandler(extStylesheetsHandler)
196-
serverMux.UnlistedHandle(extStylesheetsPath, assets.GzipHandler(extStylesheetsHandler))
197-
return nil
198-
}
199-
200-
func (c completedAssetServerConfig) addExtensionFiles(serverMux *genericmux.PathRecorderMux) {
201-
// Extension files
202-
for _, extConfig := range c.Options.Extensions {
203-
extBasePath := path.Join(c.PublicURL.Path, "extensions", extConfig.Name)
204-
extPath := extBasePath + "/"
205-
extHandler := assets.AssetExtensionHandler(extConfig.SourceDirectory, extPath, extConfig.HTML5Mode)
206-
serverMux.UnlistedHandlePrefix(extPath, http.StripPrefix(extBasePath, extHandler))
207-
serverMux.UnlistedHandle(extBasePath, http.RedirectHandler(extPath, http.StatusMovedPermanently))
208-
}
209-
}
210-
211169
func (c *completedAssetServerConfig) addWebConsoleConfig(serverMux *genericmux.PathRecorderMux) error {
212170
masterURL, err := url.Parse(c.Options.MasterPublicURL)
213171
if err != nil {
@@ -266,7 +224,7 @@ func (c completedAssetServerConfig) buildAssetHandler() (http.Handler, error) {
266224
}
267225

268226
var err error
269-
handler, err = assets.HTML5ModeHandler(c.PublicURL.Path, subcontextMap, handler, assetFunc)
227+
handler, err = assets.HTML5ModeHandler(c.PublicURL.Path, subcontextMap, c.Options.ExtensionScripts, c.Options.ExtensionStylesheets, handler, assetFunc)
270228
if err != nil {
271229
return nil, err
272230
}

pkg/assets/extensions.go

-186
This file was deleted.

pkg/assets/handlers.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"compress/gzip"
66
"encoding/hex"
77
"fmt"
8+
"html"
89
"io"
910
"net/http"
1011
"path"
@@ -112,7 +113,7 @@ func (s LongestToShortest) Less(i, j int) bool {
112113
//
113114
// subcontextMap is a map of keys (subcontexts, no leading or trailing slashes) to the asset path (no
114115
// leading slash) to serve for that subcontext if a resource that does not exist is requested
115-
func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h http.Handler, getAsset AssetFunc) (http.Handler, error) {
116+
func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, extensionScripts []string, extensionStylesheets []string, h http.Handler, getAsset AssetFunc) (http.Handler, error) {
116117
subcontextData := map[string][]byte{}
117118
subcontexts := []string{}
118119

@@ -127,6 +128,17 @@ func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h htt
127128
base += "/"
128129
}
129130
b = bytes.Replace(b, []byte(`<base href="/">`), []byte(fmt.Sprintf(`<base href="%s">`, base)), 1)
131+
132+
// Inject extension scripts and stylesheets, but only for the console itself, which has an empty subcontext
133+
if len(subcontext) == 0 {
134+
if len(extensionScripts) > 0 {
135+
b = addExtensionScripts(b, extensionScripts)
136+
}
137+
if len(extensionStylesheets) > 0 {
138+
b = addExtensionStylesheets(b, extensionStylesheets)
139+
}
140+
}
141+
130142
subcontextData[subcontext] = b
131143
subcontexts = append(subcontexts, subcontext)
132144
}
@@ -153,6 +165,30 @@ func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h htt
153165
}), nil
154166
}
155167

168+
// Add the extension scripts as the last scripts, just before the body closing tag.
169+
func addExtensionScripts(content []byte, extensionScripts []string) []byte {
170+
var scriptTags bytes.Buffer
171+
for _, scriptURL := range extensionScripts {
172+
scriptTags.WriteString(fmt.Sprintf("<script src=\"%s\"></script>\n", html.EscapeString(scriptURL)))
173+
}
174+
175+
replaceBefore := []byte("</body>")
176+
scriptTags.Write(replaceBefore)
177+
return bytes.Replace(content, replaceBefore, scriptTags.Bytes(), 1)
178+
}
179+
180+
// Add the extension stylesheets as the last stylesheets, just before the head closing tag.
181+
func addExtensionStylesheets(content []byte, extensionStylesheets []string) []byte {
182+
var styleTags bytes.Buffer
183+
for _, stylesheetURL := range extensionStylesheets {
184+
styleTags.WriteString(fmt.Sprintf("<link rel=\"stylesheet\" href=\"%s\">\n", html.EscapeString(stylesheetURL)))
185+
}
186+
187+
replaceBefore := []byte("</head>")
188+
styleTags.Write(replaceBefore)
189+
return bytes.Replace(content, replaceBefore, styleTags.Bytes(), 1)
190+
}
191+
156192
var versionTemplate = template.Must(template.New("webConsoleVersion").Parse(`
157193
window.OPENSHIFT_VERSION = {
158194
openshift: "{{ .OpenShiftVersion | js}}",

vendor/github.com/openshift/origin/pkg/cmd/server/api/validation/master.go

+9-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)