-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (55 loc) · 1.2 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
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"flag"
"fmt"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
var (
port = flag.Int("port", 50051, "The server port")
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
flag.Parse()
shutdown, err := initTracer()
if err != nil {
return err
}
defer shutdown()
server := &Server{
shouldErr: true,
}
go func() {
if err := server.Start(*port, server); err != nil {
log.Fatal(err)
}
}()
time.Sleep(time.Second)
return sayHello(*port)
}
func sayHello(port int) error {
addr := fmt.Sprintf(":%d", port)
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "world"})
if err != nil {
return fmt.Errorf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
return nil
}