Created
February 20, 2017 08:27
-
-
Save ru-rocker/8a7ef1347c073830a13b78f3819b718b to your computer and use it in GitHub Desktop.
lorem-grpc endpoints layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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