-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
54 lines (44 loc) · 1.37 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
package main
import (
"context"
"log"
"net"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pbTimeSquared "github.com/tejashwi-07/DummyGrpcServer/proto/timesquared"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type timesquaredServer struct {
pbTimeSquared.UnimplementedTimeSquaredServiceServer
}
// MaleniaService implementation
func (*timesquaredServer) HealthCheck(ctx context.Context, request *pbTimeSquared.HealthCheckRequest) (*pbTimeSquared.HealthCheckResponse, error) {
if request.RequestValue == 1 {
return &pbTimeSquared.HealthCheckResponse{HealthStatus: true}, nil
}
return &pbTimeSquared.HealthCheckResponse{HealthStatus: false}, status.Errorf(codes.Unauthenticated, "Wrong Request")
}
func main() {
// Create a listener on TCP port
lis, err := net.Listen("tcp", ":10005")
if err != nil {
log.Fatalln("Failed to listen:", err)
}
// Create a gRPC server object
s := grpc.NewServer()
pbTimeSquared.RegisterTimeSquaredServiceServer(s, ×quaredServer{})
log.Println("Serving gRPC on 0.0.0.0:10005")
go func() {
log.Fatalln(s.Serve(lis))
}()
gwmux := runtime.NewServeMux()
// Create a new HTTP server for the gRPC-Gateway
gwServer := &http.Server{
Addr: ":8094",
Handler: gwmux,
}
log.Println("Serving gRPC-Gateway on http://0.0.0.0:8094")
log.Fatalln(gwServer.ListenAndServe())
}