Created
April 29, 2021 12:17
-
-
Save mki1967/a6d94def023799ffc7f7f10bf6c933b3 to your computer and use it in GitHub Desktop.
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
/* start with: go run main.go */ | |
package main | |
import ( | |
"net/http" | |
"log" | |
"fmt" | |
) | |
func enableCors(w *http.ResponseWriter) { | |
(*w).Header().Set("Access-Control-Allow-Origin", "*") | |
} | |
var fileServer = http.FileServer(http.Dir("./")) // 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() { | |
fmt.Println("Open URL: http://localhost:8000/ in the browser") | |
fmt.Println("Type CTRL-C to stop the server.") | |
err := http.ListenAndServe(":8000", 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