/
grpc_handler.go
89 lines (68 loc) · 2.01 KB
/
grpc_handler.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package proxy
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"github.com/fabiolb/fabio/config"
"github.com/fabiolb/fabio/route"
grpc_proxy "github.com/mwitkow/grpc-proxy/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)
type GRPCServer struct {
server *grpc.Server
}
func (s *GRPCServer) Close() error {
s.server.Stop()
return nil
}
func (s *GRPCServer) Shutdown(ctx context.Context) error {
s.server.GracefulStop()
return nil
}
func (s *GRPCServer) Serve(lis net.Listener) error {
return s.server.Serve(lis)
}
func GetGRPCDirector(cfg *config.Config, tlscfg *tls.Config) func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
pick := route.Picker[cfg.Proxy.Strategy]
match := route.Matcher[cfg.Proxy.Matcher]
return func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx, nil, fmt.Errorf("error extracting metadata from request")
}
reqUrl, err := url.ParseRequestURI(fullMethodName)
if err != nil {
return ctx, nil, fmt.Errorf("error parsing request url")
}
headers := http.Header{}
for k, v := range md {
for _, h := range v {
headers.Add(k, h)
}
}
req := &http.Request{
Host: "",
URL: reqUrl,
Header: headers,
}
target := route.GetTable().Lookup(req, req.Header.Get("trace"), pick, match, cfg.GlobMatchingDisabled)
if target == nil {
return nil, nil, fmt.Errorf("no route found")
}
opts := []grpc.DialOption{
grpc.WithDefaultCallOptions(grpc.CallCustomCodec(grpc_proxy.Codec())),
}
if target.URL.Scheme == "grpcs" && tlscfg != nil {
opts = append(opts, grpc.WithTransportCredentials(
credentials.NewTLS(&tls.Config{ClientCAs: tlscfg.ClientCAs, InsecureSkipVerify: target.TLSSkipVerify})))
}
newCtx := context.Background()
conn, err := grpc.DialContext(newCtx, target.URL.Host, opts...)
return newCtx, conn, err
}
}