Package list golang-github-go-kit-kit / ec9f2b6
examples/apigateway: fix for new addsvc Peter Bourgon 4 years ago
1 changed file(s) with 12 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
1515 "syscall"
1616 "time"
1717
18 consulsd "github.com/go-kit/kit/sd/consul"
1819 "github.com/gorilla/mux"
1920 "github.com/hashicorp/consul/api"
2021 stdopentracing "github.com/opentracing/opentracing-go"
22 "google.golang.org/grpc"
2123
2224 "github.com/go-kit/kit/endpoint"
23 "github.com/go-kit/kit/examples/addsvc"
24 addsvcgrpcclient "github.com/go-kit/kit/examples/addsvc/client/grpc"
2525 "github.com/go-kit/kit/log"
2626 "github.com/go-kit/kit/sd"
27 consulsd "github.com/go-kit/kit/sd/consul"
2827 "github.com/go-kit/kit/sd/lb"
2928 httptransport "github.com/go-kit/kit/transport/http"
30 "google.golang.org/grpc"
29
30 "github.com/go-kit/kit/examples/addsvc/pkg/addendpoint"
31 "github.com/go-kit/kit/examples/addsvc/pkg/addservice"
32 "github.com/go-kit/kit/examples/addsvc/pkg/addtransport"
3133 )
3234
3335 func main() {
7880 // addsvc client package to construct a complete service. We can then
7981 // leverage the addsvc.Make{Sum,Concat}Endpoint constructors to convert
8082 // the complete service to specific endpoint.
81
8283 var (
8384 tags = []string{}
8485 passingOnly = true
85 endpoints = addsvc.Endpoints{}
86 endpoints = addendpoint.Set{}
8687 instancer = consulsd.NewInstancer(client, logger, "addsvc", tags, passingOnly)
8788 )
8889 {
89 factory := addsvcFactory(addsvc.MakeSumEndpoint, tracer, logger)
90 factory := addsvcFactory(addendpoint.MakeSumEndpoint, tracer, logger)
9091 endpointer := sd.NewEndpointer(instancer, factory, logger)
9192 balancer := lb.NewRoundRobin(endpointer)
9293 retry := lb.Retry(*retryMax, *retryTimeout, balancer)
9394 endpoints.SumEndpoint = retry
9495 }
9596 {
96 factory := addsvcFactory(addsvc.MakeConcatEndpoint, tracer, logger)
97 factory := addsvcFactory(addendpoint.MakeConcatEndpoint, tracer, logger)
9798 endpointer := sd.NewEndpointer(instancer, factory, logger)
9899 balancer := lb.NewRoundRobin(endpointer)
99100 retry := lb.Retry(*retryMax, *retryTimeout, balancer)
104105 // HTTP handler, and just install it under a particular path prefix in
105106 // our router.
106107
107 r.PathPrefix("/addsvc").Handler(http.StripPrefix("/addsvc", addsvc.MakeHTTPHandler(endpoints, tracer, logger)))
108 r.PathPrefix("/addsvc").Handler(http.StripPrefix("/addsvc", addtransport.NewHTTPHandler(endpoints, tracer, logger)))
108109 }
109110
110111 // stringsvc routes.
163164 logger.Log("exit", <-errc)
164165 }
165166
166 func addsvcFactory(makeEndpoint func(addsvc.Service) endpoint.Endpoint, tracer stdopentracing.Tracer, logger log.Logger) sd.Factory {
167 func addsvcFactory(makeEndpoint func(addservice.Service) endpoint.Endpoint, tracer stdopentracing.Tracer, logger log.Logger) sd.Factory {
167168 return func(instance string) (endpoint.Endpoint, io.Closer, error) {
168169 // We could just as easily use the HTTP or Thrift client package to make
169170 // the connection to addsvc. We've chosen gRPC arbitrarily. Note that
174175 if err != nil {
175176 return nil, nil, err
176177 }
177 service := addsvcgrpcclient.New(conn, tracer, logger)
178 service := addtransport.NewGRPCClient(conn, tracer, logger)
178179 endpoint := makeEndpoint(service)
179180
180181 // Notice that the addsvc gRPC client converts the connection to a