Skip to content

Commit

Permalink
feat(bff): proper support for SPA routes (#701)
Browse files Browse the repository at this point in the history
Signed-off-by: Eder Ignatowicz <ignatowicz@gmail.com>
  • Loading branch information
ederign authored Jan 16, 2025
1 parent 6a052c1 commit 7dcb477
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
20 changes: 16 additions & 4 deletions clients/ui/bff/internal/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/kubeflow/model-registry/ui/bff/internal/repositories"
"log/slog"
"net/http"
"path"

"github.com/julienschmidt/httprouter"
"github.com/kubeflow/model-registry/ui/bff/internal/mocks"
Expand Down Expand Up @@ -117,13 +118,24 @@ func (app *App) Routes() http.Handler {

// App Router
appMux := http.NewServeMux()

// handler for api calls
appMux.Handle("/api/v1/", apiRouter)

// file server for the frontend
staticAccessDir := http.Dir(app.config.StaticAssetsDir)
fileServer := http.FileServer(staticAccessDir)
appMux.Handle("/", fileServer)
// file server for the frontend file and SPA routes
staticDir := http.Dir(app.config.StaticAssetsDir)
fileServer := http.FileServer(staticDir)
appMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Check if the requested file exists
if _, err := staticDir.Open(r.URL.Path); err == nil {
// Serve the file if it exists
fileServer.ServeHTTP(w, r)
return
}

// Fallback to index.html for SPA routes
http.ServeFile(w, r, path.Join(app.config.StaticAssetsDir, "index.html"))
})

return app.RecoverPanic(app.enableCORS(app.InjectUserHeaders(appMux)))
}
9 changes: 7 additions & 2 deletions clients/ui/bff/internal/api/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ var _ = Describe("Static File serving Test", func() {
Expect(string(body)).To(ContainSubstring("BFF Stub Subfolder Page"))
})

It("should return 404 for a non-existent static file", func() {
It("should return index.html for a non-existent static file", func() {
resp, err := client.Get(server.URL + "/non-existent.html")
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()

Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
Expect(resp.StatusCode).To(Equal(http.StatusOK))

body, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
//BFF Stub page is the context of index.html
Expect(string(body)).To(ContainSubstring("BFF Stub Page"))
})

})
Expand Down

0 comments on commit 7dcb477

Please sign in to comment.