Newer
Older
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
package mock
import (
"context"
pb "info-manager-proxy/gen/go"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net"
)
const GrpcMockPort = 50051
type server struct {
pb.UnimplementedInformationServiceServer
}
func (s *server) GetInformation(ctx context.Context, in *pb.GrpcInformationRequest) (*pb.GrpcInformationResponse, error) {
if in.PostfachId == "" {
return nil, status.Error(codes.InvalidArgument, "PostfachId is missing")
}
if in.PostfachId == "service_unavailable" {
return nil, status.Error(codes.Unavailable, "Unavailable")
} else if in.PostfachId == "internal_server_error" {
return nil, status.Error(codes.Internal, "Error")
}
return &pb.GrpcInformationResponse{}, nil
}
func (s *server) GetInformationById(ctx context.Context, in *pb.GrpcInformationByIdRequest) (*pb.GrpcInformationByIdResponse, error) {
if in.Id == "" {
return nil, status.Error(codes.InvalidArgument, "Id is missing")
}
return &pb.GrpcInformationByIdResponse{}, nil
}
func StartGrpcServer() *grpc.Server {
s := grpc.NewServer()
pb.RegisterInformationServiceServer(s, &server{})
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", GrpcMockPort))
if err != nil {
logger.Fatal("gRPC server failed to listen: %v", err)
}
logger.Info("gRPC server listening on port %v", GrpcMockPort)
if err := s.Serve(lis); err != nil {
logger.Fatal("gRPC server failed to serve: %v", err)
}
return s
}