-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
49 lines (37 loc) · 923 Bytes
/
rpc.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
package quickdb
import (
"crypto/tls"
"github.com/riza-io/grpc-go/credentials/basic"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
pb "github.com/sqlc-dev/quickdb/v1"
)
const defaultHostname = "grpc.sqlc.dev"
type options struct {
hostname string
}
type Option func(*options)
func WithHost(host string) Option {
return func(o *options) {
o.hostname = host
}
}
func NewClient(project, token string, opts ...Option) (pb.QuickClient, error) {
var o options
for _, apply := range opts {
apply(&o)
}
dialOpts := []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(basic.NewPerRPCCredentials(project, token)),
}
hostname := o.hostname
if hostname == "" {
hostname = defaultHostname
}
conn, err := grpc.Dial(hostname+":443", dialOpts...)
if err != nil {
return nil, err
}
return pb.NewQuickClient(conn), nil
}