iris/middleware/grpc/grpc.go

31 lines
799 B
Go
Raw Permalink Normal View History

package grpc
import (
"net/http"
"strings"
2020-08-12 22:41:20 +02:00
"github.com/kataras/iris/core/router"
)
// New returns a new gRPC Iris router wrapper for a gRPC server.
// useful when you want to share one port (such as 443 for https) between gRPC and Iris.
//
// The Iris server SHOULD run under HTTP/2 and clients too.
//
// Usage:
2020-08-12 22:41:20 +02:00
// import grpcWrapper "github.com/kataras/iris/middleware/grpc"
// [...]
// app := iris.New()
// grpcServer := grpc.NewServer()
// app.WrapRouter(grpcWrapper.New(grpcServer))
func New(grpcServer http.Handler) router.WrapperFunc {
return func(w http.ResponseWriter, r *http.Request, mux http.HandlerFunc) {
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
return
}
mux.ServeHTTP(w, r)
}
}