forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
31 lines (27 loc) · 805 Bytes
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package grpc
import (
"net/http"
"strings"
"github.com/kataras/iris/v12/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:
//
// import grpcWrapper "github.com/kataras/iris/v12/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)
}
}