fixed
cors
This commit is contained in:
parent
a097fed692
commit
d54e4fa28f
@ -7,10 +7,12 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
pb "git.loafle.net/overflow/overflow_api_service/grpc"
|
pb "git.loafle.net/overflow/overflow_api_service/grpc"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
overflowEndpoint = flag.String("echo_endpoint", "localhost:9090", "/v1/overflow/services")
|
overflowEndpoint = flag.String("echo_endpoint", ":9090", "/v1/overflow/services")
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunGwRpc() (err error) {
|
func RunGwRpc() (err error) {
|
||||||
@ -27,5 +29,30 @@ func RunGwRpc() (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.ListenAndServe(":8080", mux)
|
return http.ListenAndServe(":8080", allowCORS(mux))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//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
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user