in favor of improbable-eng/grpc-web
grpc-web handler for go
Translates between grpc-web requests and grpc responses
Simply wrap your grpc server with this handler
go get github.com/seankhliao/go-grpcweb
import (
"net/http"
"google.golang.org/grpc"
grpcweb "github.com/seankhliao/go-grpcweb"
pb "your-proto-definition"
)
func main(){
svr := grpc.NewServer()
hw.RegisterGreeterServer(svr, &Server{})
// wrap grpc handler in grpc-web handler
handler := grpcweb.New(svr)
http.ListenAndServe(":8080", handler)
// OPTIONAL:
// handle cors if necessary:
// Headers:
// Access-Control-Allow-Origin
// Access-Control-Allow-Headers
// Request:
// method: OPTIONS
// response: 200
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("access-control-allow-origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
handler.ServeHTTP(w, r)
})
http.ListenAndServe(":8080", h)
}
- Write tests
- Improve error handling
- investigate closing http2 streams
- Write better docs (h2c)
- Cleanup header parsing / constants
- improbable-eng/grpc-web: similar, but incompatible
- envoyproxy/envoy: the official implementation, only for envoy, in c++