/
handler.go
executable file
·227 lines (202 loc) · 8.65 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package http
import (
"context"
"encoding/json"
"errors"
http1 "net/http"
http "github.com/go-kit/kit/transport/http"
handlers "github.com/gorilla/handlers"
mux "github.com/gorilla/mux"
endpoint "github.com/navono/gokit-todo/todo/pkg/endpoint"
)
// makeGetHandler creates the handler logic
func makeGetHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// m.Methods("POST").Path("/get").Handler(
// handlers.CORS(
// handlers.AllowedMethods([]string{"POST"}),
// handlers.AllowedOrigins([]string{"*"})
// )(http.NewServer(endpoints.GetEndpoint, decodeGetRequest, encodeGetResponse, options...)))
m.Methods("GET").Path("/").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"GET"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.GetEndpoint, decodeGetRequest, encodeGetResponse, options...)),
)
}
// decodeGetResponse is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeGetRequest(_ context.Context, r *http1.Request) (interface{}, error) {
// req := endpoint.GetRequest{}
// err := json.NewDecoder(r.Body).Decode(&req)
// return req, err
// because the request does not have any body
req := endpoint.GetRequest{}
return req, nil
}
// encodeGetResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeGetResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
if f, ok := response.(endpoint.Failure); ok && f.Failed() != nil {
ErrorEncoder(ctx, f.Failed(), w)
return nil
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeAddHandler creates the handler logic
func makeAddHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// m.Methods("POST").Path("/add").Handler(
// handlers.CORS(
// handlers.AllowedMethods([]string{"POST"}),
// handlers.AllowedOrigins([]string{"*"})
// )(http.NewServer(endpoints.AddEndpoint, decodeAddRequest, encodeAddResponse, options...)))
m.Methods("POST", "OPTIONS").Path("/add").Handler(
handlers.CORS(
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedHeaders([]string{"Content-Type", "Content-Length"}),
handlers.AllowedMethods([]string{"POST"}),
)(http.NewServer(endpoints.AddEndpoint, decodeAddRequest, encodeAddResponse, options...)))
}
// decodeAddResponse is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeAddRequest(_ context.Context, r *http1.Request) (interface{}, error) {
req := endpoint.AddRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
return req, err
}
// encodeAddResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeAddResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
if f, ok := response.(endpoint.Failure); ok && f.Failed() != nil {
ErrorEncoder(ctx, f.Failed(), w)
return nil
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeSetCompleteHandler creates the handler logic
func makeSetCompleteHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// m.Methods("POST").Path("/set-complete").Handler(
// handlers.CORS(
// handlers.AllowedMethods([]string{"POST"}),
// handlers.AllowedOrigins([]string{"*"})
// )(http.NewServer(endpoints.SetCompleteEndpoint, decodeSetCompleteRequest, encodeSetCompleteResponse, options...)))
m.Methods("PUT", "OPTIONS").Path("/set-complete").Handler(
handlers.CORS(
handlers.AllowedHeaders([]string{"Content-Type", "Content-Length"}),
handlers.AllowedMethods([]string{"PUT"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.SetCompleteEndpoint, decodeSetCompleteRequest, encodeSetCompleteResponse, options...)))
}
// decodeSetCompleteResponse is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeSetCompleteRequest(_ context.Context, r *http1.Request) (interface{}, error) {
req := endpoint.SetCompleteRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
return req, err
}
// encodeSetCompleteResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeSetCompleteResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
if f, ok := response.(endpoint.Failure); ok && f.Failed() != nil {
ErrorEncoder(ctx, f.Failed(), w)
return nil
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeRemoveCompleteHandler creates the handler logic
func makeRemoveCompleteHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// m.Methods("POST").Path("/remove-complete").Handler(
// handlers.CORS(
// handlers.AllowedMethods([]string{"POST"}),
// handlers.AllowedOrigins([]string{"*"}
// ))(http.NewServer(endpoints.RemoveCompleteEndpoint, decodeRemoveCompleteRequest, encodeRemoveCompleteResponse, options...)))
m.Methods("PUT", "OPTIONS").Path("/remove-complete").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"PUT"}),
handlers.AllowedHeaders([]string{"Content-Type", "Content-Length"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.RemoveCompleteEndpoint, decodeRemoveCompleteRequest, encodeRemoveCompleteResponse, options...)))
}
// decodeRemoveCompleteResponse is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeRemoveCompleteRequest(_ context.Context, r *http1.Request) (interface{}, error) {
req := endpoint.RemoveCompleteRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
return req, err
}
// encodeRemoveCompleteResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeRemoveCompleteResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
if f, ok := response.(endpoint.Failure); ok && f.Failed() != nil {
ErrorEncoder(ctx, f.Failed(), w)
return nil
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeDeleteHandler creates the handler logic
func makeDeleteHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// m.Methods("POST").Path("/delete").Handler(
// handlers.CORS(
// handlers.AllowedMethods([]string{"POST"}),
// handlers.AllowedOrigins([]string{"*"}
// ))(http.NewServer(endpoints.DeleteEndpoint, decodeDeleteRequest, encodeDeleteResponse, options...)))
m.Methods("DELETE", "OPTIONS").Path("/delete/{id}").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"DELETE"}),
handlers.AllowedHeaders([]string{"Content-Type", "Content-Length"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.DeleteEndpoint, decodeDeleteRequest, encodeDeleteResponse, options...)))
}
// decodeDeleteResponse is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeDeleteRequest(_ context.Context, r *http1.Request) (interface{}, error) {
// req := endpoint.DeleteRequest{}
// err := json.NewDecoder(r.Body).Decode(&req)
// return req, err
vars := mux.Vars(r)
id, ok := vars["id"]
if !ok {
return nil, errors.New("not a valid ID")
}
req := endpoint.DeleteRequest{
Id: id,
}
return req, nil
}
// encodeDeleteResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeDeleteResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
if f, ok := response.(endpoint.Failure); ok && f.Failed() != nil {
ErrorEncoder(ctx, f.Failed(), w)
return nil
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
func ErrorEncoder(_ context.Context, err error, w http1.ResponseWriter) {
w.WriteHeader(err2code(err))
json.NewEncoder(w).Encode(errorWrapper{Error: err.Error()})
}
func ErrorDecoder(r *http1.Response) error {
var w errorWrapper
if err := json.NewDecoder(r.Body).Decode(&w); err != nil {
return err
}
return errors.New(w.Error)
}
// This is used to set the http status, see an example here :
// https://github.com/go-kit/kit/blob/master/examples/addsvc/pkg/addtransport/http.go#L133
func err2code(err error) int {
return http1.StatusInternalServerError
}
type errorWrapper struct {
Error string `json:"error"`
}