How to specify CORS for http.FileServer in GO?

Hello, please tell me how to specify header headers for http.FileServer?
ie understand that you need to register it:
w.Header().Set("Access-Control-Allow-Origin", "*")

but where I do not understand...
here is the whole code:
func initServer(pathToStatic string, c chan <- string ){

 port := "46424"
 http.Handle("/static/", http.StripPrefix(strings.TrimRight("/static/", "/"), http.FileServer(http.Dir(pathToStatic))))

 log.Printf("Serving %s on HTTP port: %s\n", pathToStatic, port)
 c <- fmt.Sprintf("http://localhost:%s/static/", port)
 log.Fatal(http.ListenAndServe(":"+port, nil))
}


Thanks..
March 29th 20 at 23:50
1 answer
March 29th 20 at 23:52
Solution
Suggested solution:

var orig = http.StripPrefix("/static/", http.FileServer(http.Dir(".")))
var wrapped = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 w.Header.Set("Access-Control-Allow-Origin", "*")
 // ...

 orig.ServeHTTP(w, r)
})

http.Handle("/static/", wrapped)


Thanks Ainar-G

Find more questions by tags Web serversGo