forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (54 loc) · 1.55 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
63
64
65
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"github.com/NYTimes/gizmo/examples/servers/rpc/service"
"github.com/NYTimes/gizmo/server"
)
var (
serverAddr = flag.String("server_addr", "127.0.0.1:8080", "The server address in the format of host:port")
catsFlag = flag.Bool("cats", true, "flag make the GetCats call")
mostPopularFlag = flag.Bool("most-popular", true, "flag make the GetMostPopular call")
)
func main() {
conn, err := grpc.Dial(*serverAddr, grpc.WithInsecure())
if err != nil {
grpclog.Fatalf("fail to dial: %v", err)
}
defer func() {
err := conn.Close()
if err != nil {
server.Log.Warn("unable to close gRPC connection: ", err)
}
}()
nytClient := service.NewNYTProxyServiceClient(conn)
if *mostPopularFlag {
mostPop, err := nytClient.GetMostPopular(context.Background(), &service.MostPopularRequest{
ResourceType: "mostviewed",
Section: "all-sections",
TimePeriodDays: uint32(1),
})
if err != nil {
log.Fatal("get most popular list error: ", err)
}
fmt.Println("Most Popular Results:")
out, _ := json.MarshalIndent(mostPop, "", " ")
fmt.Fprint(os.Stdout, string(out))
fmt.Println("")
}
if *catsFlag {
cats, err := nytClient.GetCats(context.Background(), &service.CatsRequest{})
if err != nil {
log.Fatal("get cats list: ", err)
}
fmt.Println("Most Recent Articles on 'Cats':")
out, _ := json.MarshalIndent(cats, "", " ")
fmt.Fprint(os.Stdout, string(out))
}
}