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

fix http fileserver routes for console #3978

Merged
merged 2 commits into from
Aug 21, 2023
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
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)
Comment on lines +19 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks for implementing this.

}

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