-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
42 lines (35 loc) · 920 Bytes
/
transport.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
package main
import (
"encoding/json"
"net/http"
"github.com/go-kit/kit/endpoint"
"golang.org/x/net/context"
)
func makeRecordEndpoint(svc SensorService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(recordRequest)
err := svc.Record(req.I, req.V, req.T)
if err != nil {
return recordResponse{err.Error()}, nil
}
return recordResponse{""}, nil
}
}
func decodeRecordRequest(r *http.Request) (interface{}, error) {
var request recordRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
func encodeResponse(w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
type recordRequest struct {
I string `json:"i"`
V string `json:"v"`
T string `json:"t"`
}
type recordResponse struct {
Err string `json:"err,omitempty"`
}