/
main.go
89 lines (76 loc) · 2.35 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
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
// Go to ${grpc-up-and-running}/samples/ch02/productinfo
// Optional: Execute protoc -I proto proto/product_info.proto --go_out=plugins=grpc:go/product_info
// Execute go get -v github.com/grpc-up-and-running/samples/ch02/productinfo/go/product_info
// Execute go run go/server/main.go
package main
import (
"context"
"errors"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"github.com/opentracing/opentracing-go"
"log"
"net"
wrapper "github.com/golang/protobuf/ptypes/wrappers"
"github.com/google/uuid"
pb "github.com/grpc-up-and-running/samples/ch07/grpc-opentracing/go/proto"
"github.com/grpc-up-and-running/samples/ch07/grpc-opentracing/go/tracer"
"google.golang.org/grpc"
)
const (
port = ":50051"
)
// server is used to implement ecommerce/product_info.
type server struct {
productMap map[string]*pb.Product
}
// AddProduct implements ecommerce.AddProduct
func (s *server) AddProduct(ctx context.Context, in *pb.Product) (*wrapper.StringValue, error) {
out, err := uuid.NewUUID()
if err != nil {
log.Fatal(err)
}
in.Id = out.String()
if s.productMap == nil {
s.productMap = make(map[string]*pb.Product)
}
s.productMap[in.Id] = in
return &wrapper.StringValue{Value: in.Id}, nil
}
// GetProduct implements ecommerce.GetProduct
func (s *server) GetProduct(ctx context.Context, in *wrapper.StringValue) (*pb.Product, error) {
value, exists := s.productMap[in.Value]
if exists {
return value, nil
}
return nil, errors.New("Product does not exist for the ID" + in.Value)
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// Create a gRPC Server with gRPC interceptor.
grpcServer := NewServer()
pb.RegisterProductInfoServer(grpcServer, &server{})
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func NewServer() *grpc.Server {
// initialize jaegertracer
jaegertracer, closer, err := tracer.NewTracer("product_mgt")
if err != nil {
log.Fatalln(err)
return grpc.NewServer()
}
defer closer.Close()
opentracing.SetGlobalTracer(jaegertracer)
// initialize grpc server with chained interceptors
server := grpc.NewServer(
grpc.UnaryInterceptor(
// add opentracing unary interceptor to chain
grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(jaegertracer)),
),
)
return server
}