Skip to content

Instantly share code, notes, and snippets.

@dhrp
Created February 21, 2018 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrp/2203795a8db4e6fadda32ab02625cbc7 to your computer and use it in GitHub Desktop.
Save dhrp/2203795a8db4e6fadda32ab02625cbc7 to your computer and use it in GitHub Desktop.
Function to destinguish between http and gRPC
// grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
// connections or otherHandler otherwise. Copied from cockroachdb.
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment