Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve extension scripts and stylesheets as URLs #11

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 1 addition & 43 deletions pkg/assets/apiserver/asset_apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ func (c completedAssetServerConfig) New(delegationTarget genericapiserver.Delega
if err := c.addAssets(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
if err := c.addExtensionScripts(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
if err := c.addExtensionStyleSheets(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
c.addExtensionFiles(s.GenericAPIServer.Handler.NonGoRestfulMux)
if err := c.addWebConsoleConfig(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,41 +166,6 @@ func (c completedAssetServerConfig) addAssets(serverMux *genericmux.PathRecorder
return nil
}

func (c completedAssetServerConfig) addExtensionScripts(serverMux *genericmux.PathRecorderMux) error {
// Extension scripts
extScriptsPath := path.Join(c.PublicURL.Path, "scripts/extensions.js")
extScriptsHandler, err := assets.ExtensionScriptsHandler(c.Options.ExtensionScripts, c.Options.ExtensionDevelopment)
if err != nil {
return err
}
extScriptsHandler = assets.SecurityHeadersHandler(extScriptsHandler)
serverMux.UnlistedHandle(extScriptsPath, assets.GzipHandler(extScriptsHandler))
return nil
}

func (c completedAssetServerConfig) addExtensionStyleSheets(serverMux *genericmux.PathRecorderMux) error {
// Extension stylesheets
extStylesheetsPath := path.Join(c.PublicURL.Path, "styles/extensions.css")
extStylesheetsHandler, err := assets.ExtensionStylesheetsHandler(c.Options.ExtensionStylesheets, c.Options.ExtensionDevelopment)
if err != nil {
return err
}
extStylesheetsHandler = assets.SecurityHeadersHandler(extStylesheetsHandler)
serverMux.UnlistedHandle(extStylesheetsPath, assets.GzipHandler(extStylesheetsHandler))
return nil
}

func (c completedAssetServerConfig) addExtensionFiles(serverMux *genericmux.PathRecorderMux) {
// Extension files
for _, extConfig := range c.Options.Extensions {
extBasePath := path.Join(c.PublicURL.Path, "extensions", extConfig.Name)
extPath := extBasePath + "/"
extHandler := assets.AssetExtensionHandler(extConfig.SourceDirectory, extPath, extConfig.HTML5Mode)
serverMux.UnlistedHandlePrefix(extPath, http.StripPrefix(extBasePath, extHandler))
serverMux.UnlistedHandle(extBasePath, http.RedirectHandler(extPath, http.StatusMovedPermanently))
}
}

func (c *completedAssetServerConfig) addWebConsoleConfig(serverMux *genericmux.PathRecorderMux) error {
masterURL, err := url.Parse(c.Options.MasterPublicURL)
if err != nil {
Expand Down Expand Up @@ -266,7 +224,7 @@ func (c completedAssetServerConfig) buildAssetHandler() (http.Handler, error) {
}

var err error
handler, err = assets.HTML5ModeHandler(c.PublicURL.Path, subcontextMap, handler, assetFunc)
handler, err = assets.HTML5ModeHandler(c.PublicURL.Path, subcontextMap, c.Options.ExtensionScripts, c.Options.ExtensionStylesheets, handler, assetFunc)
if err != nil {
return nil, err
}
Expand Down
186 changes: 0 additions & 186 deletions pkg/assets/extensions.go

This file was deleted.

38 changes: 37 additions & 1 deletion pkg/assets/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"encoding/hex"
"fmt"
"html"
"io"
"net/http"
"path"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (s LongestToShortest) Less(i, j int) bool {
//
// subcontextMap is a map of keys (subcontexts, no leading or trailing slashes) to the asset path (no
// leading slash) to serve for that subcontext if a resource that does not exist is requested
func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h http.Handler, getAsset AssetFunc) (http.Handler, error) {
func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, extensionScripts []string, extensionStylesheets []string, h http.Handler, getAsset AssetFunc) (http.Handler, error) {
subcontextData := map[string][]byte{}
subcontexts := []string{}

Expand All @@ -127,6 +128,17 @@ func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h htt
base += "/"
}
b = bytes.Replace(b, []byte(`<base href="/">`), []byte(fmt.Sprintf(`<base href="%s">`, base)), 1)

// Inject extension scripts and stylesheets, but only for the console itself, which has an empty subcontext
if len(subcontext) == 0 {
if len(extensionScripts) > 0 {
b = addExtensionScripts(b, extensionScripts)
}
if len(extensionStylesheets) > 0 {
b = addExtensionStylesheets(b, extensionStylesheets)
}
}

subcontextData[subcontext] = b
subcontexts = append(subcontexts, subcontext)
}
Expand All @@ -153,6 +165,30 @@ func HTML5ModeHandler(contextRoot string, subcontextMap map[string]string, h htt
}), nil
}

// Add the extension scripts as the last scripts, just before the body closing tag.
func addExtensionScripts(content []byte, extensionScripts []string) []byte {
var scriptTags bytes.Buffer
for _, scriptURL := range extensionScripts {
scriptTags.WriteString(fmt.Sprintf("<script src=\"%s\"></script>\n", html.EscapeString(scriptURL)))
}

replaceBefore := []byte("</body>")
scriptTags.Write(replaceBefore)
return bytes.Replace(content, replaceBefore, scriptTags.Bytes(), 1)
}

// Add the extension stylesheets as the last stylesheets, just before the head closing tag.
func addExtensionStylesheets(content []byte, extensionStylesheets []string) []byte {
var styleTags bytes.Buffer
for _, stylesheetURL := range extensionStylesheets {
styleTags.WriteString(fmt.Sprintf("<link rel=\"stylesheet\" href=\"%s\">\n", html.EscapeString(stylesheetURL)))
}

replaceBefore := []byte("</head>")
styleTags.Write(replaceBefore)
return bytes.Replace(content, replaceBefore, styleTags.Bytes(), 1)
}

var versionTemplate = template.Must(template.New("webConsoleVersion").Parse(`
window.OPENSHIFT_VERSION = {
openshift: "{{ .OpenShiftVersion | js}}",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.