mirror of
https://github.com/status-im/simulation.git
synced 2025-02-22 11:58:06 +00:00
Add CORS support
This commit is contained in:
parent
2e30599707
commit
01d186ae2c
20
cmd/propagation_simulator/cors.go
Normal file
20
cmd/propagation_simulator/cors.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
|
||||
func allowCORS(fn func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if origin := r.Header.Get("Access-Control-Allow-Origin"); origin == "" {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||
if origin := r.Header.Get("Origin"); origin != "" {
|
||||
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
|
||||
// set preflight options
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fn(w, r)
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ func main() {
|
||||
|
||||
if *server {
|
||||
log.Println("Starting simulator server on", *serverAddr)
|
||||
http.HandleFunc("/", simulationHandler)
|
||||
http.HandleFunc("/", allowCORS(simulationHandler))
|
||||
log.Fatal(http.ListenAndServe(*serverAddr, nil))
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user