forked from mwitkow/grpc-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
32 lines (26 loc) · 965 Bytes
/
proxy.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
// Copyright 2021 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package proxy
import (
"google.golang.org/grpc"
)
// NewProxy sets up a simple proxy that forwards all requests to dst.
func NewProxy(dst *grpc.ClientConn, opts ...grpc.ServerOption) *grpc.Server {
opts = append(opts, DefaultProxyOpt(dst))
// Set up the proxy server and then serve from it like in step one.
return grpc.NewServer(opts...)
}
// DefaultProxyOpt returns an grpc.UnknownServiceHandler with a DefaultDirector.
func DefaultProxyOpt(cc *grpc.ClientConn) grpc.ServerOption {
return grpc.UnknownServiceHandler(TransparentHandler(DefaultDirector(cc), DefaultErrorHandler()))
}
// DefaultDirector returns a very simple forwarding StreamDirector that forwards all
// calls.
func DefaultDirector(cc *grpc.ClientConn) StreamDirector {
return func() string {
return cc.Target()
}
}
func DefaultErrorHandler() ErrorHandler {
return func(addr string) {}
}