/
handler.go
43 lines (35 loc) · 1.4 KB
/
handler.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 http
import (
"context"
"encoding/json"
"net/http"
kithttp "github.com/go-kit/kit/transport/http"
api "github.com/teamlint/mons/sample2/services/user/api"
"github.com/teamlint/mons/sample2/services/user/internal/endpoint"
)
func NewHTTPHandler(endpoints endpoint.Endpoints, options map[string][]kithttp.ServerOption) http.Handler {
m := http.NewServeMux()
makeFindHandler(m, endpoints, options["Find"])
makeUpdateHandler(m, endpoints, options["Update"])
return m
}
func makeFindHandler(m *http.ServeMux, endpoints endpoint.Endpoints, options []kithttp.ServerOption) {
m.Handle("/user/find", kithttp.NewServer(endpoints.FindEndpoint, decodeFindRequest, kithttp.EncodeJSONResponse, options...))
}
func makeUpdateHandler(m *http.ServeMux, endpoints endpoint.Endpoints, options []kithttp.ServerOption) {
m.Handle("/user/update", kithttp.NewServer(endpoints.UpdateEndpoint, decodeUpdateRequest, kithttp.EncodeJSONResponse, options...))
}
func decodeFindRequest(ctx context.Context, req *http.Request) (interface{}, error) {
var request api.FindUserRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
return nil, err
}
return &request, nil
}
func decodeUpdateRequest(ctx context.Context, req *http.Request) (interface{}, error) {
var request api.UpdateUserRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
return nil, err
}
return &request, nil
}