Skip to content

Commit

Permalink
[+] allow CORS from http://localhost:4000 for webui debugging (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub authored May 2, 2024
1 parent 441163b commit c94d225
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/webserver/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package webserver

import (
"net/http"
)

func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:4000") //check src/webui/.env
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, token")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
2 changes: 1 addition & 1 deletion src/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Init(opts config.WebUIOpts, webuifs fs.FS, mrw metrics.ReaderWriter, srw so
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: mux,
Handler: corsMiddleware(mux),
},
opts,
webuifs,
Expand Down

0 comments on commit c94d225

Please sign in to comment.