-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.go
49 lines (39 loc) · 1.32 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
package main
import (
"context"
"log"
gw "github.com/johanbrandhorst/grpc-wasm"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
pb "github.com/mycodesmells/golang-examples/grpc/proto/service"
)
func main() {
creds, err := credentials.NewClientTLSFromFile("cmd/server/server-cert.pem", "")
if err != nil {
log.Fatalf("cert load error: %s", err)
}
conn, err := gw.Dial("localhost:6000", grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("Failed to start gRPC connection: %v", err)
}
defer conn.Close()
client := pb.NewSimpleServerClient(conn)
md := metadata.Pairs("token", "valid-token")
ctx := metadata.NewOutgoingContext(context.Background(), md)
_, err = client.CreateUser(ctx, &pb.CreateUserRequest{User: &pb.User{Username: "slomek", Role: "joker"}}, grpc.With)
if err != nil {
log.Fatalf("Failed to create user: %v", err)
}
log.Println("Created user!")
resp, err := client.GetUser(ctx, &pb.GetUserRequest{Username: "slomek"})
if err != nil {
log.Fatalf("Failed to get created user: %v", err)
}
log.Printf("User exists: %v\n", resp)
resp2, err := client.GreetUser(ctx, &pb.GreetUserRequest{Greeting: "howdy", Username: "slomek"})
if err != nil {
log.Fatalf("Failed to greet user: %v", err)
}
log.Printf("Greeting: %s\n", resp2.Greeting)
}