-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
144 lines (120 loc) · 2.86 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"flag"
"fmt"
"grpc_tutorial/pb"
"io"
"log"
"os"
"path/filepath"
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const port = ":9000"
func main() {
absoluteFilePath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
cert := absoluteFilePath + "/cert.pem"
option := flag.Int("o", 1, "Command to execute")
flag.Parse()
creds, err := credentials.NewClientTLSFromFile(cert, "")
if err != nil {
log.Fatal(err)
}
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
conn, err := grpc.Dial("localhost"+port, opts...)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewUserServiceClient(conn)
switch *option {
case 1:
SendMetaData(client)
case 2:
GetUserById(client)
case 3:
GetAllUsers(client)
case 4:
Save(client)
case 5:
SaveAll(client)
default:
SendMetaData(client)
}
}
func SendMetaData(client pb.UserServiceClient) {
md := metadata.MD{}
md["username"] = []string{"amulya"}
md["password"] = []string{"itisnotauthenticated"}
ctx := context.Background()
ctx = metadata.NewOutgoingContext(ctx, md)
client.GetUserById(ctx, &pb.UserByIdPayload{})
}
func GetUserById(client pb.UserServiceClient) {
res, err := client.GetUserById(context.Background(), &pb.UserByIdPayload{Id: 1})
if err != nil {
log.Fatal(err)
}
fmt.Println("user data : ", res.User)
}
func GetAllUsers(client pb.UserServiceClient) {
stream, err := client.GetAllUsers(context.Background(), &pb.AllUsersPayload{})
if err != nil {
log.Fatal(err)
}
for {
res, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println("user data is : ", res.User)
}
}
func Save(client pb.UserServiceClient) {
new_user := new(pb.User)
new_user.Id = 3
new_user.Name = "Amulya3"
new_user.Email = "amulya3@gmail.com"
new_user.Password = "imnotwithyouguysanymore"
res, err := client.Save(context.Background(), &pb.UserPayload{User: new_user})
if err != nil {
log.Fatal(err)
}
fmt.Println("user data : ", res.User)
}
func SaveAll(client pb.UserServiceClient) {
users := []pb.User{
pb.User{Id: 99, Name: "Amulya99", Email: "amulya99@gmail.com", Password: "iamthepassword"},
pb.User{Id: 98, Name: "Amulya98", Email: "amulya98@gmail.com", Password: "iamthepassword"},
pb.User{Id: 97, Name: "Amulya97", Email: "amulya97@gmail.com", Password: "iamthepassword"},
}
stream, err := client.SaveAll(context.Background())
if err != nil {
log.Fatal(err)
}
doneChannel := make(chan struct{})
go func() {
for {
res, err := stream.Recv()
if err == io.EOF {
doneChannel <- struct{}{}
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println("user data is : ", res.User)
}
}()
for _, u := range users {
err := stream.Send(&pb.UserPayload{User: &u})
if err != nil {
log.Fatal(err)
}
}
}