Skip to content

Instantly share code, notes, and snippets.

@f3nry f3nry/main.go
Last active Feb 12, 2019

Embed
What would you like to do?
Simple Go gRPC Client
package main
import (
"context"
"flag"
"fmt"
"log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"api"
)
var (
tls = flag.Bool("tls", true, "Connection uses TLS if true, else plain TCP")
caFile = flag.String("ca_file", "", "The file containning the CA root cert file")
serverAddr = flag.String("server_addr", "<api-gateway-host>:443", "The server address in the format of host:port")
serverHostOverride = flag.String("server_host_override", "<api-gateway-host>", "The server name use to verify the hostname returned by TLS handshake")
)
func main() {
flag.Parse()
var opts []grpc.DialOption
if *tls {
creds, err := credentials.NewClientTLSFromFile(*caFile, *serverHostOverride)
if err != nil {
log.Fatalf("Failed to create TLS credentials %v", err)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
opts = append(opts, grpc.WithWaitForHandshake())
} else {
opts = append(opts, grpc.WithInsecure())
}
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := api.NewProdClient(conn)
resp, err := client.Alive(context.TODO(), &api.AliveRequest{Message: "Hello"})
if err != nil {
log.Fatalf("err making request: %v", err)
}
fmt.Println(resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.