Created
February 21, 2018 17:27
-
-
Save dhrp/2203795a8db4e6fadda32ab02625cbc7 to your computer and use it in GitHub Desktop.
Function to destinguish between http and gRPC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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