/
endpoints.go
59 lines (48 loc) · 1.13 KB
/
endpoints.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
package lorem_rate_limit
import (
"github.com/go-kit/kit/endpoint"
"strings"
"errors"
"context"
//"golang.org/x/net/context"
)
var (
ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph")
)
//request
type LoremRequest struct {
RequestType string
Min int
Max int
}
//response
type LoremResponse struct {
Message string `json:"message"`
Err error `json:"err,omitempty"`
}
// endpoints wrapper
type Endpoints struct {
LoremEndpoint endpoint.Endpoint
}
// creating Lorem Ipsum Endpoint
func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(LoremRequest)
var (
txt string
min, max int
)
min = req.Min
max = req.Max
if strings.EqualFold(req.RequestType, "Word") {
txt = svc.Word(min, max)
} else if strings.EqualFold(req.RequestType, "Sentence"){
txt = svc.Sentence(min, max)
} else if strings.EqualFold(req.RequestType, "Paragraph") {
txt = svc.Paragraph(min, max)
} else {
return nil, ErrRequestTypeNotFound
}
return LoremResponse{Message: txt}, nil
}
}