/
accnt.go
72 lines (62 loc) · 2.13 KB
/
accnt.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
60
61
62
63
64
65
66
67
68
69
70
71
72
package http
import (
"context"
"encoding/json"
stdhttp "net/http"
"strconv"
"github.com/AyushSenapati/reactive-micro/authnsvc/pkg/dto"
"github.com/AyushSenapati/reactive-micro/authnsvc/pkg/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
)
// makeCreateAccountHandler creates the handler logic
func makeCreateAccountHandler(m *mux.Router, endpoints endpoint.Endpoints, options []kithttp.ServerOption) {
m.Methods("POST").Path("/accounts").Handler(
kithttp.NewServer(
endpoints.CreateAccountEndpoint,
decodeCreateAccountRequest,
encodeHTTPGenericResponse,
options...,
))
}
// decodeCreateAccountRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeCreateAccountRequest(_ context.Context, r *stdhttp.Request) (interface{}, error) {
req := dto.CreateAccountRequest{}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&req)
return req, err
}
// makeDeleteAccountHandler creates the handler logic
func makeDeleteAccountHandler(m *mux.Router, endpoints endpoint.Endpoints, options []kithttp.ServerOption) {
m.Methods("DELETE").Path("/accounts/{aid:[0-9]+}").Handler(
kithttp.NewServer(
endpoints.DeleteAccountEndpoint,
decodeDeleteUserRequest,
encodeHTTPGenericResponse,
options...))
}
// decodeDeleteAccountRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeDeleteUserRequest(_ context.Context, r *stdhttp.Request) (interface{}, error) {
// Get account ID from the URI
vars := mux.Vars(r)
aid, err := strconv.Atoi(vars["aid"])
if err != nil {
return nil, err
}
return uint(aid), err
}
func makeListAccountHandler(m *mux.Router, endpoints endpoint.Endpoints, options []kithttp.ServerOption) {
m.Methods("GET").Path("/accounts").Handler(
kithttp.NewServer(
endpoints.ListAccountEndpoint,
decodeListResouceRequest,
encodeHTTPGenericResponse,
options...))
}
func decodeListResouceRequest(_ context.Context, r *stdhttp.Request) (interface{}, error) {
qp := processBasicQP(r)
return qp, nil
}