Last active
May 23, 2019 19:47
-
-
Save nekonenene/2adcccd13e20b8d536c156ea96d82a22 to your computer and use it in GitHub Desktop.
Client for my gRPC server(ブログ公開用) https://nekonenene.hatenablog.com/entry/grpc-nginx-proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"crypto/tls" | |
"log" | |
"os" | |
"strconv" | |
"time" | |
pb "nekonenene/hello/pb" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/credentials" | |
) | |
const ( | |
address = "grpc-test.hatone.net:443" | |
defaultName = "Kaname" | |
defaultAge = 20 | |
) | |
func main() { | |
// Set up a connection to the server. | |
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}) | |
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds)) | |
if err != nil { | |
log.Fatalf("did not connect: %v", err) | |
} | |
defer conn.Close() | |
c := pb.NewGreeterClient(conn) | |
name := defaultName | |
age := defaultAge | |
if len(os.Args) > 2 { | |
name = os.Args[1] | |
age, _ = strconv.Atoi(os.Args[2]) | |
} | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | |
defer cancel() | |
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name, Age: int32(age)}) | |
if err != nil { | |
log.Fatalf("could not get message: %v, %v", err, r) | |
} | |
log.Printf("Got message: %s", r.Message) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment