-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (60 loc) · 1.79 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"net"
"strings"
"github.com/mwitkow/grpc-proxy/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
var store = make(map[string]*grpc.ClientConn)
func main() {
lis, err := net.Listen("tcp", ":50052")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer(
grpc.UnknownServiceHandler(proxy.TransparentHandler(director)),
)
log.Println("Serving gRPC on 0.0.0.0:50052")
log.Fatal(s.Serve(lis))
}
func director(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
fmt.Println("fullMethodName: ", fullMethodName)
md, _ := metadata.FromIncomingContext(ctx)
var header metadata.MD
if val, exists := md["authorization"]; exists {
header = metadata.New(map[string]string{
"authorization": val[0],
})
}
ctx = metadata.NewOutgoingContext(ctx, header)
var conn *grpc.ClientConn
var err error
switch {
case strings.HasPrefix(fullMethodName, "/helloworld.Greeter/"):
conn, err = getConnection(ctx, store, "localhost:8080")
case strings.HasPrefix(fullMethodName, "/auth.AuthService/"):
conn, err = getConnection(ctx, store, "localhost:8081")
default:
err = status.Errorf(codes.Unimplemented, "Unknown method")
}
return ctx, conn, err
}
func getConnection(ctx context.Context, store map[string]*grpc.ClientConn, address string) (*grpc.ClientConn, error) {
// If connection exists, return it.
if conn, ok := store[address]; ok {
return conn, nil
}
// If connection does not exist, create a new one and store it.
newConn, err := grpc.DialContext(ctx, address, grpc.WithCodec(proxy.Codec()), grpc.WithInsecure())
if err != nil {
return nil, err
}
store[address] = newConn
return newConn, nil
}