Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Created February 20, 2017 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ru-rocker/8a7ef1347c073830a13b78f3819b718b to your computer and use it in GitHub Desktop.
Save ru-rocker/8a7ef1347c073830a13b78f3819b718b to your computer and use it in GitHub Desktop.
lorem-grpc endpoints layer
package lorem_grpc
import (
"github.com/go-kit/kit/endpoint"
"context"
"errors"
)
//request
type LoremRequest struct {
RequestType string
Min int32
Max int32
}
//response
type LoremResponse struct {
Message string `json:"message"`
Err string `json:"err,omitempty"`
}
// endpoints wrapper
type Endpoints struct {
LoremEndpoint endpoint.Endpoint
}
// creating Lorem Ipsum Endpoint
func MakeLoremEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(LoremRequest)
var (
min, max int
)
min = int(req.Min)
max = int(req.Max)
txt, err := svc.Lorem(ctx, req.RequestType, min, max)
if err != nil {
return nil, err
}
return LoremResponse{Message: txt}, nil
}
}
// Wrapping Endpoints as a Service implementation.
// Will be used in gRPC client
func (e Endpoints) Lorem(ctx context.Context, requestType string, min, max int) (string, error) {
req := LoremRequest{
RequestType: requestType,
Min: int32(min),
Max: int32(max),
}
resp, err := e.LoremEndpoint(ctx, req)
if err != nil {
return "", err
}
loremResp := resp.(LoremResponse)
if loremResp.Err != "" {
return "", errors.New(loremResp.Err)
}
return loremResp.Message, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment