Skip to content

Instantly share code, notes, and snippets.

@mkdym
Created August 10, 2023 13:56
Show Gist options
  • Save mkdym/195b8d15ac4ff6f178f708f98f39021d to your computer and use it in GitHub Desktop.
Save mkdym/195b8d15ac4ff6f178f708f98f39021d to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/http"
)
func CORS(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Credentials", "true")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Add("Cross-Origin-Opener-Policy", "same-origin")
w.Header().Add("Cross-Origin-Embedder-Policy", "require-corp")
if r.Method == "OPTIONS" {
http.Error(w, "No Content", http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
}
}
func main() {
fs := http.FileServer(http.Dir("D:/Godot/Projects/Dodge"))
http.Handle("/", CORS(fs))
log.Print("Listening on :3000...")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment