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

Applying same console fix for the subpath issue #1616

Merged
merged 1 commit into from
Jun 12, 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
18 changes: 10 additions & 8 deletions api/configure_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ var (
subPathOnce sync.Once
)

func getSubPath() string {
// GetSubPath is the sub-path where Operator UI will run
func GetSubPath() string {
subPathOnce.Do(func() {
subPath = parseSubPath(env.Get(SubPath, ""))
})
Expand Down Expand Up @@ -317,7 +318,7 @@ func replaceBaseInIndex(indexPageBytes []byte, basePath string) []byte {

// handleSPA handles the serving of the React Single Page Application
func handleSPA(w http.ResponseWriter, r *http.Request) {
basePath := "/"
basePath := GetSubPath()
// For SPA mode we will replace root base with a sub path if configured unless we received cp=y and cpb=/NEW/BASE
if v := r.URL.Query().Get("cp"); v == "y" {
if base := r.URL.Query().Get("cpb"); base != "" {
Expand All @@ -344,11 +345,11 @@ func handleSPA(w http.ResponseWriter, r *http.Request) {
}

// if we have a seeded basePath. This should override CONSOLE_SUBPATH every time, thus the `if else`
if basePath != "/" {
if basePath != GetSubPath() {
indexPageBytes = replaceBaseInIndex(indexPageBytes, basePath)
// if we have a custom subpath replace it in
} else if getSubPath() != "/" {
indexPageBytes = replaceBaseInIndex(indexPageBytes, getSubPath())
} else if GetSubPath() != "/" {
indexPageBytes = replaceBaseInIndex(indexPageBytes, GetSubPath())
}
indexPageBytes = replaceLicense(indexPageBytes)

Expand All @@ -365,7 +366,7 @@ func handleSPA(w http.ResponseWriter, r *http.Request) {
// wrapHandlerSinglePageApplication handles a http.FileServer returning a 404 and overrides it with index.html
func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
if match, _ := regexp.MatchString(fmt.Sprintf("^%s/?$", GetSubPath()), r.URL.Path); match {
handleSPA(w, r)
return
}
Expand All @@ -383,15 +384,16 @@ func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
func FileServerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", globalAppName) // do not add version information
basePath := GetSubPath()
switch {
case strings.HasPrefix(r.URL.Path, "/api"):
case strings.HasPrefix(r.URL.Path, basePath+"api"):
next.ServeHTTP(w, r)
default:
buildFs, err := fs.Sub(webApp.GetStaticAssets(), "build")
if err != nil {
panic(err)
}
wrapHandlerSinglePageApplication(requestBounce(http.FileServer(http.FS(buildFs)))).ServeHTTP(w, r)
wrapHandlerSinglePageApplication(requestBounce(http.StripPrefix(basePath, http.FileServer(http.FS(buildFs))))).ServeHTTP(w, r)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/operator/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func buildOperatorServer() (*api.Server, error) {
return nil, err
}

subPath := api.GetSubPath()
swaggerSpec.Spec().BasePath = subPath + swaggerSpec.Spec().BasePath
swaggerSpec.OrigSpec().BasePath = subPath + swaggerSpec.OrigSpec().BasePath

operatorapi := operations.NewOperatorAPI(swaggerSpec)
operatorapi.Logger = api.LogInfo
server := api.NewServer(operatorapi)
Expand Down
1 change: 1 addition & 0 deletions web-app/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Api, Error, FullRequestParams, HttpResponse } from "./operatorApi";

export let api = new Api();
const internalRequestFunc = api.request;
api.baseUrl = `${new URL(document.baseURI).pathname}api/v1`;
api.request = async <T = any, E = any>({
body,
secure,
Expand Down