Skip to content

Commit

Permalink
[GEN-1743]: serve root file for "404 file not found" (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenElferink authored Nov 20, 2024
1 parent 44be3a8 commit 67af0aa
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,21 @@ func startHTTPServer(flags *Flags, odigosMetrics *collectormetrics.OdigosMetrics
}

func httpFileServerWith404(fs http.FileSystem) http.Handler {
// Init outside of handler to respect manipulated paths
fileServer := http.FileServer(fs)

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fs.Open(r.URL.Path)
path := r.URL.Path

// Check if the requested file exists
_, err := fs.Open(path)
if err != nil {
// Serve index.html
r.URL.Path = "/"
// Redirect to root path
r.URL.Path = "/index.html"
}
http.FileServer(fs).ServeHTTP(w, r)

// Serve the file
fileServer.ServeHTTP(w, r)
})
}

Expand Down

0 comments on commit 67af0aa

Please sign in to comment.