From 383b11435b2ef4975958fc46f84d8f268b3b760b Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 7 Aug 2017 13:10:34 -0700 Subject: [PATCH] Document Server.ServeHTTP (#1406) Fixes #549 --- server.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/server.go b/server.go index 5e9da3d9..8ee07fe7 100644 --- a/server.go +++ b/server.go @@ -588,6 +588,29 @@ func (s *Server) serveUsingHandler(conn net.Conn) { }) } +// ServeHTTP implements the Go standard library's http.Handler +// interface by responding to the gRPC request r, by looking up +// the requested gRPC method in the gRPC server s. +// +// The provided HTTP request must have arrived on an HTTP/2 +// connection. When using the Go standard library's server, +// practically this means that the Request must also have arrived +// over TLS. +// +// To share one port (such as 443 for https) between gRPC and an +// existing http.Handler, use a root http.Handler such as: +// +// if r.ProtoMajor == 2 && strings.HasPrefix( +// r.Header.Get("Content-Type"), "application/grpc") { +// grpcServer.ServeHTTP(w, r) +// } else { +// yourMux.ServeHTTP(w, r) +// } +// +// Note that ServeHTTP uses Go's HTTP/2 server implementation which is +// totally separate from grpc-go's HTTP/2 server. Performance and +// features may vary between the two paths. ServeHTTP may not respect +// all provided grpc.ServerOption values. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { st, err := transport.NewServerHandlerTransport(w, r) if err != nil {