Skip to content

Commit

Permalink
Merge pull request #18 from ncdc/1.5-rebase-simplify-asset-mux
Browse files Browse the repository at this point in the history
Simplify asset handler mux
  • Loading branch information
ncdc authored Dec 15, 2016
2 parents 715e44c + 5f93b10 commit 32e4364
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 46 deletions.
73 changes: 27 additions & 46 deletions pkg/cmd/server/origin/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,17 @@ func (c *AssetConfig) WithAssets(handler http.Handler) (http.Handler, error) {
return handler, nil
}

publicURL, err := url.Parse(c.Options.PublicURL)
if err != nil {
return nil, err
}

mux := http.NewServeMux()
err = c.addHandlers(mux)
if err != nil {
return nil, err
}

if !strings.HasSuffix(publicURL.Path, "/") {
publicURL.Path = publicURL.Path + "/"
}

return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.URL.Path + "/", publicURL.Path) {
mux.ServeHTTP(w, req)
} else {
handler.ServeHTTP(w, req)
}
}), nil
return c.addHandlers(handler)
}

// Run starts an http server for the static assets listening on the configured
// bind address
func (c *AssetConfig) Run() {
publicURL, err := url.Parse(c.Options.PublicURL)
if err != nil {
glog.Fatal(err)
}

mux := http.NewServeMux()
err = c.addHandlers(mux)
mux, err := c.addHandlers(nil)
if err != nil {
glog.Fatal(err)
}

if publicURL.Path != "/" {
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, publicURL.Path, http.StatusFound)
})
}

timeout := c.Options.ServingInfo.RequestTimeoutSeconds
if timeout == -1 {
timeout = 0
Expand Down Expand Up @@ -168,20 +135,34 @@ func extensionPropertyArray(extensionProperties map[string]string) []assets.WebC
return extensionPropsArray
}

func (c *AssetConfig) addHandlers(mux *http.ServeMux) error {
assetHandler, err := c.buildAssetHandler()
func (c *AssetConfig) addHandlers(handler http.Handler) (http.Handler, error) {
publicURL, err := url.Parse(c.Options.PublicURL)
if err != nil {
return err
return nil, err
}

publicURL, err := url.Parse(c.Options.PublicURL)
mux := http.NewServeMux()
if handler != nil {
// colocated with other routes, so pass any unrecognized routes through to
// handler
mux.Handle("/", handler)
} else {
// standalone mode, so redirect any unrecognized routes to the console
if publicURL.Path != "/" {
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, publicURL.Path, http.StatusFound)
})
}
}

assetHandler, err := c.buildAssetHandler()
if err != nil {
return err
return nil, err
}

masterURL, err := url.Parse(c.Options.MasterPublicURL)
if err != nil {
return err
return nil, err
}

// Web console assets
Expand Down Expand Up @@ -220,7 +201,7 @@ func (c *AssetConfig) addHandlers(mux *http.ServeMux) error {
}
}
if commonResources.Len() > 0 {
return fmt.Errorf("Resources for kubernetes and origin types intersect: %v", commonResources.List())
return nil, fmt.Errorf("Resources for kubernetes and origin types intersect: %v", commonResources.List())
}

// Generated web console config and server version
Expand Down Expand Up @@ -255,23 +236,23 @@ func (c *AssetConfig) addHandlers(mux *http.ServeMux) error {
configPath := path.Join(publicURL.Path, "config.js")
configHandler, err := assets.GeneratedConfigHandler(config, versionInfo, extensionProps)
if err != nil {
return err
return nil, err
}
mux.Handle(configPath, assets.GzipHandler(configHandler))

// Extension scripts
extScriptsPath := path.Join(publicURL.Path, "scripts/extensions.js")
extScriptsHandler, err := assets.ExtensionScriptsHandler(c.Options.ExtensionScripts, c.Options.ExtensionDevelopment)
if err != nil {
return err
return nil, err
}
mux.Handle(extScriptsPath, assets.GzipHandler(extScriptsHandler))

// Extension stylesheets
extStylesheetsPath := path.Join(publicURL.Path, "styles/extensions.css")
extStylesheetsHandler, err := assets.ExtensionStylesheetsHandler(c.Options.ExtensionStylesheets, c.Options.ExtensionDevelopment)
if err != nil {
return err
return nil, err
}
mux.Handle(extStylesheetsPath, assets.GzipHandler(extStylesheetsHandler))

Expand All @@ -283,5 +264,5 @@ func (c *AssetConfig) addHandlers(mux *http.ServeMux) error {
mux.Handle(extPath, http.StripPrefix(extBasePath, extHandler))
}

return nil
return mux, nil
}
38 changes: 38 additions & 0 deletions test/integration/web_console_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,41 @@ func TestAccessOriginWebConsoleMultipleIdentityProviders(t *testing.T) {
tryAccessURL(t, url, exp.statusCode, exp.location, nil)
}
}

func TestAccessStandaloneOriginWebConsole(t *testing.T) {
testutil.RequireEtcd(t)
defer testutil.DumpEtcdOnFailure(t)

masterOptions, err := testserver.DefaultMasterOptions()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

addr, err := testserver.FindAvailableBindAddress(13000, 13999)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

masterOptions.AssetConfig.ServingInfo.BindAddress = addr
assetBaseURL := "https://" + addr
masterOptions.AssetConfig.PublicURL = assetBaseURL + "/console/"
masterOptions.OAuthConfig.AssetPublicURL = assetBaseURL + "/console/"

if _, err = testserver.StartConfiguredMaster(masterOptions); err != nil {
t.Fatalf("unexpected error: %v", err)
}

for endpoint, exp := range map[string]struct {
statusCode int
location string
}{
"": {http.StatusFound, "/console/"},
"blarg": {http.StatusFound, "/console/"},
"console": {http.StatusMovedPermanently, "/console/"},
"console/": {http.StatusOK, ""},
"console/java": {http.StatusOK, ""},
} {
url := assetBaseURL + "/" + endpoint
tryAccessURL(t, url, exp.statusCode, exp.location, nil)
}
}

0 comments on commit 32e4364

Please sign in to comment.