-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (141 loc) · 5.49 KB
/
main.go
File metadata and controls
169 lines (141 loc) · 5.49 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"context"
"encoding/json"
"fmt"
commentpb "gen/comment-service/pb"
communitypb "gen/community-service/pb"
popularpb "gen/popular-service/pb"
searchpb "gen/search-service/pb"
threadpb "gen/thread-service/pb"
votepb "gen/vote-service/pb"
"log"
"net/http"
"os"
gorun "runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/emptypb"
)
func getGrpcServerAddress(hostEnvVar string, portEnvVar string) string {
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)
}
return fmt.Sprintf("%s:%s", host, port)
}
func connectGrpcClient(hostEnvVar string, portEnvVar string) *grpc.ClientConn {
addr := getGrpcServerAddress(hostEnvVar, portEnvVar)
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 handleHealthCheck(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
health := map[string]bool{
"community-service": false,
"thread-service": false,
"comment-service": false,
"vote-service": false,
"search-service": false,
"popular-service": false,
}
communityConn := connectGrpcClient("COMMUNITY_SERVICE_HOST", "COMMUNITY_SERVICE_PORT")
defer communityConn.Close()
communityClient := communitypb.NewCommunityServiceClient(communityConn)
_, err := communityClient.CheckHealth(ctx, &emptypb.Empty{})
health["community-service"] = err == nil
threadConn := connectGrpcClient("THREAD_SERVICE_HOST", "THREAD_SERVICE_PORT")
defer threadConn.Close()
threadClient := threadpb.NewThreadServiceClient(threadConn)
_, err = threadClient.CheckHealth(ctx, &emptypb.Empty{})
health["thread-service"] = err == nil
commentConn := connectGrpcClient("COMMENT_SERVICE_HOST", "COMMENT_SERVICE_PORT")
defer commentConn.Close()
commentClient := commentpb.NewCommentServiceClient(commentConn)
_, err = commentClient.CheckHealth(ctx, &emptypb.Empty{})
health["comment-service"] = err == nil
voteConn := connectGrpcClient("VOTE_SERVICE_HOST", "VOTE_SERVICE_PORT")
defer voteConn.Close()
voteClient := votepb.NewVoteServiceClient(voteConn)
_, err = voteClient.CheckHealth(ctx, &emptypb.Empty{})
health["vote-service"] = err == nil
searchConn := connectGrpcClient("SEARCH_SERVICE_HOST", "SEARCH_SERVICE_PORT")
defer searchConn.Close()
searchClient := searchpb.NewSearchServiceClient(searchConn)
_, err = searchClient.CheckHealth(ctx, &emptypb.Empty{})
health["search-service"] = err == nil
popularConn := connectGrpcClient("POPULAR_SERVICE_HOST", "POPULAR_SERVICE_PORT")
defer popularConn.Close()
popularClient := popularpb.NewPopularServiceClient(popularConn)
_, err = popularClient.CheckHealth(ctx, &emptypb.Empty{})
health["popular-service"] = err == nil
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonResponse, err := json.Marshal(health)
if err != nil {
http.Error(w, "Failed to marshal JSON response", http.StatusInternalServerError)
return
}
w.Write(jsonResponse)
}
func main() {
// Set maximum number of CPUs to use
gorun.GOMAXPROCS(gorun.NumCPU())
gwmux := runtime.NewServeMux()
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(1024*1024*500), // 500MB
grpc.MaxCallSendMsgSize(1024*1024*500), // 500MB
),
}
err := communitypb.RegisterCommunityServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("COMMUNITY_SERVICE_HOST", "COMMUNITY_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
err = threadpb.RegisterThreadServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("THREAD_SERVICE_HOST", "THREAD_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
err = commentpb.RegisterCommentServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("COMMENT_SERVICE_HOST", "COMMENT_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
err = votepb.RegisterVoteServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("VOTE_SERVICE_HOST", "VOTE_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
err = searchpb.RegisterSearchServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("SEARCH_SERVICE_HOST", "SEARCH_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
err = popularpb.RegisterPopularServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("POPULAR_SERVICE_HOST", "POPULAR_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
http.HandleFunc("/health", handleHealthCheck)
http.Handle("/", gwmux)
port := os.Getenv("GRPC_GATEWAY_PORT")
if port == "" {
log.Fatalf("missing GRPC_GATEWAY_PORT env var")
}
log.Printf("gRPC Gateway server listening on :%s", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
log.Fatalf("Failed to start HTTP server: %v", err)
}
}