/
transports_metric.go
44 lines (37 loc) · 998 Bytes
/
transports_metric.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
package transports
import (
"GoURL/arithmetic_rest_multi_endpoints/endpoints"
"context"
"encoding/json"
"github.com/gorilla/mux"
"net/http"
"strconv"
)
// decodeArithmeticRequestMath decode request params to struct
func decodeArithmeticRequestMath(_ context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
requestType, ok := vars["type"]
if !ok {
return nil, ErrorBadRequestMath
}
pa, ok := vars["a"]
if !ok {
return nil, ErrorBadRequestMath
}
pb, ok := vars["b"]
if !ok {
return nil, ErrorBadRequestMath
}
a, _ := strconv.Atoi(pa)
b, _ := strconv.Atoi(pb)
return endpoints.ArithmeticRequest{
RequestType: requestType,
A: a,
B: b,
}, nil
}
// encodeArithmeticResponseMath encode response to return
func encodeArithmeticResponseMath(ctx context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set("Content-Type", "application/json;charset=utf-8")
return json.NewEncoder(w).Encode(response)
}