DevOpsGOServer
/
service.go
166 строк · 3.7 Кб
1package main
2
3import (
4"context"
5"encoding/json"
6"errors"
7"log"
8"net/http"
9"strings"
10
11"github.com/go-kit/kit/endpoint"
12)
13
14type StringService interface {
15Uppercase(string) (string, error)
16Count(string) int
17}
18
19type Service interface {
20Uppercase(ctx context.Context, s string) (string, error)
21Count(ctx context.Context, s string) int
22}
23
24type stringService struct{}
25
26func (stringService) Uppercase(s string) (string, error) {
27if s == "" {
28return "", ErrEmpty
29}
30return strings.ToUpper(s), nil
31
32}
33
34func (stringService) Count(s string) int {
35return len(s)
36}
37
38var ErrEmpty = errors.New("empty string")
39
40var _ StringService = (*stringService)(nil) // compile-time interface check for StringService implementation
41
42type uppercaseRequest struct {
43S string `json:"s"`
44}
45
46type uppercaseResponse struct {
47V string `json:"v"`
48Err string `json:"err,omitempty"`
49
50}
51
52type uppercaseResponse struct {
53V string `json:"v"`
54Err string `json:"err,omitempty"`
55}
56
57type countRequest struct {
58S string `json:"s"`
59}
60
61type countResponse struct {
62V int `json:"v"`
63}
64
65func countScore(scores []int) int {
66total := 0
67for _, score := range scores {
68total += score
69}
70return total
71}
72
73func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
74return func(_ context.Context, request interface{}) (interface{}, error) {
75req := request.(uppercaseRequest)
76v, err := svc.Uppercase(req.S)
77if err != nil {
78return uppercaseResponse{v, err.Error()}, nil
79}
80
81resp := uppercaseResponse{v, ""}
82log.Println("Request: ", req.S, ", Response: ", resp.V)
83return resp, nil
84}
85}
86
87func makeCountEndpoint(svc StringService) endpoint.Endpoint {
88return func(_ context.Context, request interface{}) (interface{}, error) {
89req := request.(countRequest)
90v := svc.Count(req.S)
91
92resp := countResponse{v}
93log.Println("Request: ", req.S, ", Response: ", resp.V)
94return resp, nil
95}
96}
97
98func decodeUppercaseRequest(_ context.Context, r *http.Request) (interface{}, error) {
99var request uppercaseRequest
100if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
101return nil, err
102}
103return request, nil
104}
105
106func decodeUppercaseResponse(_ context.Context, r *http.Request) (interface{}, error) {
107var response uppercaseResponse
108if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
109return nil, err
110}
111return response, nil
112
113}
114
115func decodeCountRequest(_ context.Context, r *http.Request) (interface{}, error) {
116var request countRequest
117if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
118return nil, err
119}
120return request, nil
121
122}
123
124func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
125return json.NewEncoder(w).Encode(response)
126
127}
128
129func decodeCountRequest(_ context.Context, r *http.Request) (interface{}, error) {
130var request countRequest
131if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
132return nil, err
133}
134return request, nil
135}
136
137func decodeUppercaseRequest(_ context.Context, r *http.Request) (interface{}, error) {
138var request uppercaseRequest
139if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
140return nil, err
141}
142return request, nil
143
144}
145
146func decodeUppercaseResponse(_ context.Context, r *http.Request) (interface{}, error) {
147var response uppercaseResponse
148if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
149return nil, err
150}
151return response, nil
152
153}
154
155func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
156return json.NewEncoder(w).Encode(response)
157}
158
159func decodeCountResponse(_ context.Context, r *http.Request) (interface{}, error) {
160var response countResponse
161if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
162return nil, err
163}
164return response, nil
165
166}
167