Skip to content

Commit

Permalink
fix http fileserver routes for console (#3978)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeev B <[email protected]>
  • Loading branch information
jeevb authored Aug 21, 2023
1 parent 2f4edd7 commit 36c81df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
32 changes: 14 additions & 18 deletions cmd/single/console.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package single

import (
"path/filepath"
"net/http"
"strings"
)

const consoleRoot = "/console"
const assetsDir = "assets"
const packageDir = "dist"
const indexHTML = "/index.html"
const (
consoleRoot = "/console"
consoleStatic = consoleRoot + "/assets/"
packageDir = "dist"
indexHTML = "index.html"
)

// GetConsoleFile returns the console file that should be used for the given path.
// Every path has a '/console' as the prefix. After dropping this prefix, the following 3 rules are checked
// Rule 1: If path is now "" or "/" then return index.html
// Rule 2: If path contains no "assets" sub-string and has an additional substring "/", then return "index.html".
// This is to allow vanity urls in React
// Rule 3: Finally return every file path as is
// For every file add a "dist" as the prefix, as every file is assumed to be packaged in "dist" folder fv
func GetConsoleFile(name string) string {
name = strings.TrimPrefix(name, consoleRoot)
name = strings.TrimPrefix(name, "/"+assetsDir)
if name == "" || name == "/" {
name = indexHTML
} else if !strings.Contains(name, assetsDir) {
if strings.Contains(name[1:], "/") {
name = indexHTML
}
// Serve requests for static assets at `/console/assets/<filename>`
// as-is from `dist`
if strings.HasPrefix(name, consoleStatic) {
return filepath.Join(packageDir, strings.TrimPrefix(name, consoleStatic))
}
return packageDir + name

// Send all other requests to `index.html` to be handled by react router
return filepath.Join(packageDir, indexHTML)
}

type consoleFS struct {
Expand Down
8 changes: 4 additions & 4 deletions cmd/single/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ func TestGetConsoleFile(t *testing.T) {
}{
{"/console", "dist/index.html"},
{"/console/", "dist/index.html"},
{"/console/main.js", "dist/main.js"},
{"/console/assets/xyz.png", "dist/assets/xyz.png"},
{"/console/assets/dir/xyz.png", "dist/assets/dir/xyz.png"},
{"console/projects/flytesnacks/workflows?domain=development", "dist/index.html"},
{"/console/assets/xyz.png", "dist/xyz.png"},
{"/console/assets/dir/xyz.png", "dist/dir/xyz.png"},
{"/console/projects/flytesnacks/workflows?domain=development", "dist/index.html"},
{"/console/select-project", "dist/index.html"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 36c81df

Please sign in to comment.