-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (70 loc) · 2.03 KB
/
main.go
File metadata and controls
81 lines (70 loc) · 2.03 KB
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
package main
import (
server "comment-service/src"
"fmt"
commentpb "gen/comment-service/pb"
dbpb "gen/db-service/pb"
threadpb "gen/thread-service/pb"
"log"
"net"
"os"
"runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func connectGrpcClient(hostEnvVar string, portEnvVar string) *grpc.ClientConn {
host := os.Getenv(hostEnvVar)
if host == "" {
log.Fatalf("missing %s env var", hostEnvVar)
}
port := os.Getenv(portEnvVar)
if port == "" {
log.Fatalf("missing %s env var", portEnvVar)
}
addr := fmt.Sprintf("%s:%s", host, port)
conn, err := grpc.NewClient(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(1024*1024*500), // 500MB
grpc.MaxCallSendMsgSize(1024*1024*500), // 500MB
),
)
if err != nil {
log.Fatalf("failed to connect to %s: %v", addr, err)
}
return conn
}
func main() {
// Set maximum number of CPUs to use
runtime.GOMAXPROCS(runtime.NumCPU())
// connect to database service
dbConn := connectGrpcClient("DB_SERVICE_HOST", "DB_SERVICE_PORT")
defer dbConn.Close()
// connect to thread service
threadConn := connectGrpcClient("THREAD_SERVICE_HOST", "THREAD_SERVICE_PORT")
defer threadConn.Close()
// create comment service with database service and thread
commentService := &server.CommentServer{
DBClient: dbpb.NewDBServiceClient(dbConn),
ThreadClient: threadpb.NewThreadServiceClient(threadConn),
}
// get env port
port := os.Getenv("SERVICE_PORT")
if port == "" {
log.Fatalf("missing SERVICE_PORT env var")
}
// start gRPC server
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer(
grpc.MaxRecvMsgSize(1024*1024*500), // 500MB
grpc.MaxSendMsgSize(1024*1024*500), // 500MB
)
commentpb.RegisterCommentServiceServer(grpcServer, commentService)
log.Printf("gRPC server is listening on :%s", port)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}