Skip to content

Instantly share code, notes, and snippets.

@mki1967
Created April 29, 2021 12:17
Show Gist options
  • Save mki1967/a6d94def023799ffc7f7f10bf6c933b3 to your computer and use it in GitHub Desktop.
Save mki1967/a6d94def023799ffc7f7f10bf6c933b3 to your computer and use it in GitHub Desktop.
/* 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