Created
August 9, 2018 11:37
-
-
Save mki1967/933008187ed22e7e1b737bcba7026655 to your computer and use it in GitHub Desktop.
go server for CORS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* srever of data accessible by CORS */ | |
/* start with: go run main.go */ | |
package main | |
import ( | |
"net/http" | |
"log" | |
) | |
func enableCors(w *http.ResponseWriter) { | |
(*w).Header().Set("Access-Control-Allow-Origin", "*") | |
} | |
var fileServer = http.FileServer(http.Dir("/path-to-your-files")) // instance of file server handler | |
type myHandler struct{} | |
func (myHandler) ServeHTTP(w http.ResponseWriter, req *http.Request){ | |
enableCors(&w) | |
fileServer.ServeHTTP(w, req) | |
} | |
func main() { | |
err := http.ListenAndServe(":8001", new(myHandler) ) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment