/
init.go
48 lines (41 loc) · 1.18 KB
/
init.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
package handler
import (
"flag"
"log"
pb "github.com/Coreychen4444/shortvideo"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
user_addr = flag.String("addr", "user_service:50051", "the address to connect to")
video_addr = flag.String("addr", "video_service:50052", "the address to connect to")
)
func UserClient() *pb.UserServiceClient {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*user_addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewUserServiceClient(conn)
return &c
}
func VideoClient() *pb.VideoServiceClient {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*video_addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewVideoServiceClient(conn)
return &c
}
type ServiceHandler struct {
uc pb.UserServiceClient
vc pb.VideoServiceClient
}
func NewServiceHandler(uc *pb.UserServiceClient, vc *pb.VideoServiceClient) *ServiceHandler {
return &ServiceHandler{uc: *uc, vc: *vc}
}