kit: github.com/go-kit/kit/examples/addsvc Index | Files | Directories

package addsvc

import "github.com/go-kit/kit/examples/addsvc"

Package addsvc is an example microservice, useful for education. It can sum integers and concatenate strings. A client library is available in the client subdirectory. A server binary is available in cmd/addsrv. An example client binary is available in cmd/addcli.

Index

Package Files

doc.go endpoints.go service.go transport_grpc.go transport_http.go transport_thrift.go

Variables

var (
    // ErrTwoZeroes is an arbitrary business rule for the Add method.
    ErrTwoZeroes = errors.New("can't sum two zeroes")

    // ErrIntOverflow protects the Add method. We've decided that this error
    // indicates a misbehaving service and should count against e.g. circuit
    // breakers. So, we return it directly in endpoints, to illustrate the
    // difference. In a real service, this probably wouldn't be the case.
    ErrIntOverflow = errors.New("integer overflow")

    // ErrMaxSizeExceeded protects the Concat method.
    ErrMaxSizeExceeded = errors.New("result exceeds maximum size")
)

func DecodeGRPCConcatRequest Uses

func DecodeGRPCConcatRequest(_ context.Context, grpcReq interface{}) (interface{}, error)

DecodeGRPCConcatRequest is a transport/grpc.DecodeRequestFunc that converts a gRPC concat request to a user-domain concat request. Primarily useful in a server.

func DecodeGRPCConcatResponse Uses

func DecodeGRPCConcatResponse(_ context.Context, grpcReply interface{}) (interface{}, error)

DecodeGRPCConcatResponse is a transport/grpc.DecodeResponseFunc that converts a gRPC concat reply to a user-domain concat response. Primarily useful in a client.

func DecodeGRPCSumRequest Uses

func DecodeGRPCSumRequest(_ context.Context, grpcReq interface{}) (interface{}, error)

DecodeGRPCSumRequest is a transport/grpc.DecodeRequestFunc that converts a gRPC sum request to a user-domain sum request. Primarily useful in a server.

func DecodeGRPCSumResponse Uses

func DecodeGRPCSumResponse(_ context.Context, grpcReply interface{}) (interface{}, error)

DecodeGRPCSumResponse is a transport/grpc.DecodeResponseFunc that converts a gRPC sum reply to a user-domain sum response. Primarily useful in a client.

func DecodeHTTPConcatRequest Uses

func DecodeHTTPConcatRequest(_ context.Context, r *http.Request) (interface{}, error)

DecodeHTTPConcatRequest is a transport/http.DecodeRequestFunc that decodes a JSON-encoded concat request from the HTTP request body. Primarily useful in a server.

func DecodeHTTPConcatResponse Uses

func DecodeHTTPConcatResponse(_ context.Context, r *http.Response) (interface{}, error)

DecodeHTTPConcatResponse is a transport/http.DecodeResponseFunc that decodes a JSON-encoded concat response from the HTTP response body. If the response has a non-200 status code, we will interpret that as an error and attempt to decode the specific error message from the response body. Primarily useful in a client.

func DecodeHTTPSumRequest Uses

func DecodeHTTPSumRequest(_ context.Context, r *http.Request) (interface{}, error)

DecodeHTTPSumRequest is a transport/http.DecodeRequestFunc that decodes a JSON-encoded sum request from the HTTP request body. Primarily useful in a server.

func DecodeHTTPSumResponse Uses

func DecodeHTTPSumResponse(_ context.Context, r *http.Response) (interface{}, error)

DecodeHTTPSumResponse is a transport/http.DecodeResponseFunc that decodes a JSON-encoded sum response from the HTTP response body. If the response has a non-200 status code, we will interpret that as an error and attempt to decode the specific error message from the response body. Primarily useful in a client.

func EncodeGRPCConcatRequest Uses

func EncodeGRPCConcatRequest(_ context.Context, request interface{}) (interface{}, error)

EncodeGRPCConcatRequest is a transport/grpc.EncodeRequestFunc that converts a user-domain concat request to a gRPC concat request. Primarily useful in a client.

func EncodeGRPCConcatResponse Uses

func EncodeGRPCConcatResponse(_ context.Context, response interface{}) (interface{}, error)

EncodeGRPCConcatResponse is a transport/grpc.EncodeResponseFunc that converts a user-domain concat response to a gRPC concat reply. Primarily useful in a server.

func EncodeGRPCSumRequest Uses

func EncodeGRPCSumRequest(_ context.Context, request interface{}) (interface{}, error)

EncodeGRPCSumRequest is a transport/grpc.EncodeRequestFunc that converts a user-domain sum request to a gRPC sum request. Primarily useful in a client.

func EncodeGRPCSumResponse Uses

func EncodeGRPCSumResponse(_ context.Context, response interface{}) (interface{}, error)

EncodeGRPCSumResponse is a transport/grpc.EncodeResponseFunc that converts a user-domain sum response to a gRPC sum reply. Primarily useful in a server.

func EncodeHTTPGenericRequest Uses

func EncodeHTTPGenericRequest(_ context.Context, r *http.Request, request interface{}) error

EncodeHTTPGenericRequest is a transport/http.EncodeRequestFunc that JSON-encodes any request to the request body. Primarily useful in a client.

func EncodeHTTPGenericResponse Uses

func EncodeHTTPGenericResponse(_ context.Context, w http.ResponseWriter, response interface{}) error

EncodeHTTPGenericResponse is a transport/http.EncodeResponseFunc that encodes the response as JSON to the response writer. Primarily useful in a server.

func EndpointInstrumentingMiddleware Uses

func EndpointInstrumentingMiddleware(duration metrics.Histogram) endpoint.Middleware

EndpointInstrumentingMiddleware returns an endpoint middleware that records the duration of each invocation to the passed histogram. The middleware adds a single field: "success", which is "true" if no error is returned, and "false" otherwise.

func EndpointLoggingMiddleware Uses

func EndpointLoggingMiddleware(logger log.Logger) endpoint.Middleware

EndpointLoggingMiddleware returns an endpoint middleware that logs the duration of each invocation, and the resulting error, if any.

func MakeConcatEndpoint Uses

func MakeConcatEndpoint(s Service) endpoint.Endpoint

MakeConcatEndpoint returns an endpoint that invokes Concat on the service. Primarily useful in a server.

func MakeGRPCServer Uses

func MakeGRPCServer(endpoints Endpoints, tracer stdopentracing.Tracer, logger log.Logger) pb.AddServer

MakeGRPCServer makes a set of endpoints available as a gRPC AddServer.

func MakeHTTPHandler Uses

func MakeHTTPHandler(endpoints Endpoints, tracer stdopentracing.Tracer, logger log.Logger) http.Handler

MakeHTTPHandler returns a handler that makes a set of endpoints available on predefined paths.

func MakeSumEndpoint Uses

func MakeSumEndpoint(s Service) endpoint.Endpoint

MakeSumEndpoint returns an endpoint that invokes Sum on the service. Primarily useful in a server.

func MakeThriftConcatEndpoint Uses

func MakeThriftConcatEndpoint(client *thriftadd.AddServiceClient) endpoint.Endpoint

MakeThriftConcatEndpoint returns an endpoint that invokes the passed Thrift client. Useful only in clients, and only until a proper transport/thrift.Client exists.

func MakeThriftHandler Uses

func MakeThriftHandler(ctx context.Context, e Endpoints) thriftadd.AddService

MakeThriftHandler makes a set of endpoints available as a Thrift service.

func MakeThriftSumEndpoint Uses

func MakeThriftSumEndpoint(client *thriftadd.AddServiceClient) endpoint.Endpoint

MakeThriftSumEndpoint returns an endpoint that invokes the passed Thrift client. Useful only in clients, and only until a proper transport/thrift.Client exists.

type Endpoints Uses

type Endpoints struct {
    SumEndpoint    endpoint.Endpoint
    ConcatEndpoint endpoint.Endpoint
}

Endpoints collects all of the endpoints that compose an add service. It's meant to be used as a helper struct, to collect all of the endpoints into a single parameter.

In a server, it's useful for functions that need to operate on a per-endpoint basis. For example, you might pass an Endpoints to a function that produces an http.Handler, with each method (endpoint) wired up to a specific path. (It is probably a mistake in design to invoke the Service methods on the Endpoints struct in a server.)

In a client, it's useful to collect individually constructed endpoints into a single type that implements the Service interface. For example, you might construct individual endpoints using transport/http.NewClient, combine them into an Endpoints, and return it to the caller as a Service.

func (Endpoints) Concat Uses

func (e Endpoints) Concat(ctx context.Context, a, b string) (string, error)

Concat implements Service. Primarily useful in a client.

func (Endpoints) Sum Uses

func (e Endpoints) Sum(ctx context.Context, a, b int) (int, error)

Sum implements Service. Primarily useful in a client.

type Middleware Uses

type Middleware func(Service) Service

Middleware describes a service (as opposed to endpoint) middleware.

func ServiceInstrumentingMiddleware Uses

func ServiceInstrumentingMiddleware(ints, chars metrics.Counter) Middleware

ServiceInstrumentingMiddleware returns a service middleware that instruments the number of integers summed and characters concatenated over the lifetime of the service.

func ServiceLoggingMiddleware Uses

func ServiceLoggingMiddleware(logger log.Logger) Middleware

ServiceLoggingMiddleware returns a service middleware that logs the parameters and result of each method invocation.

type Service Uses

type Service interface {
    Sum(ctx context.Context, a, b int) (int, error)
    Concat(ctx context.Context, a, b string) (string, error)
}

Service describes a service that adds things together.

func NewBasicService Uses

func NewBasicService() Service

NewBasicService returns a naïve, stateless implementation of Service.

Directories

PathSynopsis
client/grpcPackage grpc provides a gRPC client for the add service.
client/httpPackage http provides an HTTP client for the add service.
client/thriftPackage thrift provides a Thrift client for the add service.
pbPackage pb is a generated protocol buffer package.
thrift/gen-go/addsvc
thrift/gen-go/addsvc/add_service-remote

Package addsvc imports 18 packages (graph) and is imported by 5 packages. Updated 2017-03-13. Refresh now. Tools for package owners.