Skip to content

Commit b5cd6b4

Browse files
backend: Add finalizeHandler function and setupCORS
Signed-off-by: SinghaAnirban005 <[email protected]>
1 parent 1c4c304 commit b5cd6b4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

backend/cmd/headlamp.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
oidc "github.com/coreos/go-oidc/v3/oidc"
4242
"github.com/gobwas/glob"
4343
"github.com/google/uuid"
44+
"github.com/gorilla/handlers"
4445
"github.com/gorilla/mux"
4546
"github.com/kubernetes-sigs/headlamp/backend/pkg/auth"
4647
"github.com/kubernetes-sigs/headlamp/backend/pkg/cache"
@@ -904,6 +905,40 @@ func processAndWriteProxyResponse(w http.ResponseWriter, resp *http.Response) er
904905
return nil
905906
}
906907

908+
func finalizeHandler(config *HeadlampConfig, router *mux.Router) http.Handler {
909+
if config.StaticDir != "" {
910+
staticPath := config.StaticDir
911+
if isWindows && strings.Contains(config.StaticDir, "/") {
912+
staticPath = filepath.FromSlash(config.StaticDir)
913+
}
914+
915+
spa := spaHandler{
916+
staticPath: staticPath,
917+
indexPath: "index.html",
918+
baseURL: config.BaseURL,
919+
}
920+
router.PathPrefix("/").Handler(spa)
921+
}
922+
923+
if config.DevMode {
924+
return setupCORS(router)
925+
}
926+
927+
return router
928+
}
929+
930+
func setupCORS(router *mux.Router) http.Handler {
931+
headers := handlers.AllowedHeaders([]string{
932+
"X-HEADLAMP_BACKEND-TOKEN", "X-Requested-With", "Content-Type",
933+
"Authorization", "Forward-To",
934+
"KUBECONFIG", "X-HEADLAMP-USER-ID",
935+
})
936+
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "DELETE", "PATCH", "OPTIONS"})
937+
origins := handlers.AllowedOrigins([]string{"*"})
938+
939+
return handlers.CORS(headers, methods, origins)(router)
940+
}
941+
907942
func getResponseReader(resp *http.Response) (io.ReadCloser, error) {
908943
switch resp.Header.Get("Content-Encoding") {
909944
case "gzip":

0 commit comments

Comments
 (0)