fixed cross domain
This commit is contained in:
parent
e77cd9f604
commit
f66f96304d
28
main.go
28
main.go
|
@ -8,6 +8,8 @@ import (
|
||||||
|
|
||||||
"git.loafle.net/overflow/overflow_service_websocket/config"
|
"git.loafle.net/overflow/overflow_service_websocket/config"
|
||||||
"git.loafle.net/overflow/overflow_service_websocket/server"
|
"git.loafle.net/overflow/overflow_service_websocket/server"
|
||||||
|
"strings"
|
||||||
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -53,7 +55,7 @@ func main() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
http.Handle("/rpc", ws.HTTPHandler())
|
http.Handle("/rpc", allowCORS(ws.HTTPHandler()))
|
||||||
log.Printf("Address: %s, UseTLS: %t", addr, useTLS)
|
log.Printf("Address: %s, UseTLS: %t", addr, useTLS)
|
||||||
|
|
||||||
if err := http.ListenAndServe(addr, nil); err != nil {
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
||||||
|
@ -61,6 +63,30 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/main.go
|
||||||
|
|
||||||
|
func allowCORS(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if origin := r.Header.Get("Origin"); origin != "" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
|
||||||
|
preflightHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func preflightHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
headers := []string{"Content-Type", "Accept"}
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
|
||||||
|
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
|
||||||
|
glog.Infof("preflight request for %s", r.URL.Path)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func loadConfig() *config.Config {
|
func loadConfig() *config.Config {
|
||||||
os.Chdir("./")
|
os.Chdir("./")
|
||||||
wd, _ := os.Getwd()
|
wd, _ := os.Getwd()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user