Skip to content
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
3 changes: 2 additions & 1 deletion assets/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dist
.sass-cache
.bundle
bower_components
phantomjsdriver.log
phantomjsdriver.log
openshift-jvm
6 changes: 6 additions & 0 deletions assets/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ module.exports = function (grunt) {
cwd: 'bower_components/zeroclipboard/dist',
src: 'ZeroClipboard.swf',
dest: '<%= yeoman.dist %>/scripts'
},
{
expand: true,
cwd: 'openshift-jvm',
src: '**/*',
dest: '<%= yeoman.dist %>/java'
}]
},
styles: {
Expand Down
4 changes: 4 additions & 0 deletions hack/install-assets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pushd ${OS_ROOT}/assets > /dev/null
# In case upstream components change things without incrementing versions
cmd "bower cache clean --allow-root"
cmd "bower install --allow-root"

cmd "rm -rf openshift-jvm"
cmd "mkdir -p openshift-jvm"
wget https://github.com/hawtio/openshift-jvm/archive/v1.0.7-build.tar.gz -O - | tar -xz -C openshift-jvm --strip-components=1
popd > /dev/null

pushd ${OS_ROOT}/Godeps/_workspace > /dev/null
Expand Down
2,130 changes: 2,130 additions & 0 deletions pkg/assets/bindata.go

Large diffs are not rendered by default.

58 changes: 50 additions & 8 deletions pkg/assets/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"html/template"
"io"
"net/http"
"path"
"regexp"
"sort"
"strings"

"github.com/golang/glog"
Expand Down Expand Up @@ -77,21 +79,61 @@ func CacheControlHandler(version string, h http.Handler) http.Handler {
})
}

type LongestToShortest []string

func (s LongestToShortest) Len() int {
return len(s)
}
func (s LongestToShortest) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s LongestToShortest) Less(i, j int) bool {
return len(s[i]) > len(s[j])
}

// HTML5ModeHandler will serve any static assets we know about, all other paths
// are assumed to be HTML5 paths for the console application and index.html will
// be served.
// contextRoot must contain leading and trailing slashes, e.g. /console/
func HTML5ModeHandler(contextRoot string, h http.Handler) (http.Handler, error) {
b, err := Asset("index.html")
if err != nil {
return nil, err
//
// 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) (http.Handler, error) {
subcontextData := map[string][]byte{}
subcontexts := []string{}

for subcontext, index := range subcontextMap {
b, err := Asset(index)
if err != nil {
return nil, err
}
base := path.Join(contextRoot, subcontext)
// Make sure the base always ends in a trailing slash but don't end up with a double trailing slash
if !strings.HasSuffix(base, "/") {
base += "/"
}
b = bytes.Replace(b, []byte(`<base href="/">`), []byte(fmt.Sprintf(`<base href="%s">`, base)), 1)
subcontextData[subcontext] = b
subcontexts = append(subcontexts, subcontext)
}
b = bytes.Replace(b, []byte(`<base href="/">`), []byte(fmt.Sprintf(`<base href="%s">`, contextRoot)), 1)

// Sort by length, longest first
sort.Sort(LongestToShortest(subcontexts))

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := Asset(strings.TrimPrefix(r.URL.Path, "/")); err != nil {
w.Write(b)
return
urlPath := strings.TrimPrefix(r.URL.Path, "/")
if _, err := Asset(urlPath); err != nil {
// find the index we want to serve instead
for _, subcontext := range subcontexts {
prefix := subcontext
if subcontext != "" {
prefix += "/"
}
if urlPath == subcontext || strings.HasPrefix(urlPath, prefix) {
w.Write(subcontextData[subcontext])
return
}
}
}
h.ServeHTTP(w, r)
}), nil
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/server/origin/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ func (c *AssetConfig) buildHandler() (http.Handler, error) {
},
)

handler, err = assets.HTML5ModeHandler(publicURL.Path, handler)
// Map of context roots (no leading or trailing slash) to the asset path to serve for requests to a missing asset
subcontextMap := map[string]string{
"": "index.html",
"java": "java/index.html",
}

handler, err = assets.HTML5ModeHandler(publicURL.Path, subcontextMap, handler)
if err != nil {
return nil, err
}
Expand Down