-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp_handler.go
299 lines (261 loc) · 7.5 KB
/
http_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package logpeck
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/go-zoo/bone"
)
func logRequest(r *http.Request, prefix string) {
str, _ := httputil.DumpRequest(r, true)
log.Infof("[Handler] [%s] req_len[%d] req[%s]", prefix, len(str), str)
}
// NewAddTaskHandler .
func NewAddTaskHandler(pecker *Pecker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "AddTaskHandler")
defer r.Body.Close()
var config PeckTaskConfig
raw, _ := ioutil.ReadAll(r.Body)
err := config.Unmarshal(raw)
if err != nil {
log.Infof("[Handler] Parse PeckTaskConfig error, %s", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Bad Request, %s in %v", err, string(raw[:]))))
return
}
err = pecker.AddPeckTask(&config, nil)
if err != nil {
log.Infof("[Handler] AddTaskConfig error, %s", err)
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Add failed, " + err.Error()))
return
}
log.Infof("[Handler] Add Success: %s", raw)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Add Success"))
return
}
}
// NewUpdateTaskHandler .
func NewUpdateTaskHandler(pecker *Pecker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "UpdateTaskHandler")
defer r.Body.Close()
var config PeckTaskConfig
raw, _ := ioutil.ReadAll(r.Body)
err := config.Unmarshal(raw)
if err != nil {
log.Infof("[Handler] Parse PeckTaskConfig error, %s", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Bad Request, %s in %v", err, string(raw[:]))))
return
}
err = pecker.UpdatePeckTask(&config)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Update failed, " + err.Error()))
return
}
if err != nil {
log.Infof("[Handler] UpdateTaskConfig error, save config error, %s", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
log.Infof("[Handler] Update Success: %s", raw)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Update Success"))
}
}
// NewStartTaskHandler .
func NewStartTaskHandler(pecker *Pecker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "StartTaskHandler")
defer r.Body.Close()
var config PeckTaskConfig
raw, _ := ioutil.ReadAll(r.Body)
err := config.Unmarshal(raw)
if err != nil {
log.Infof("[Handler] Start PeckTask error, %s", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request, " + err.Error()))
return
}
err = pecker.StartPeckTask(&config)
if err != nil {
log.Infof("[Handler] Start PeckTask error, %s", err.Error())
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Start failed, " + err.Error()))
return
}
log.Infof("[Handler] Start Success: %s", raw)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Start Success"))
}
}
// NewStopTaskHandler .
func NewStopTaskHandler(pecker *Pecker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "StopTaskHandler")
defer r.Body.Close()
var config PeckTaskConfig
raw, _ := ioutil.ReadAll(r.Body)
err := config.Unmarshal(raw)
if err != nil {
log.Infof("[Handler] Stop PeckTask error, %s", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request, " + err.Error()))
return
}
err = pecker.StopPeckTask(&config)
if err != nil {
log.Infof("[Handler] Stop PeckTask error, %s", err.Error())
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Stop failed, " + err.Error()))
return
}
log.Infof("[Handler] Stop Success: %s", raw)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Stop Success"))
}
}
// NewRemoveTaskHandler .
func NewRemoveTaskHandler(pecker *Pecker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "RemoveTaskHandler")
defer r.Body.Close()
var config PeckTaskConfig
raw, _ := ioutil.ReadAll(r.Body)
err := config.Unmarshal(raw)
if err != nil {
log.Infof("[Handler] Remove PeckTask error, %s", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request, " + err.Error()))
return
}
err = pecker.RemovePeckTask(&config)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Remove PeckTask failed, " + err.Error()))
return
}
log.Infof("[Handler] Remove Success: %s", raw)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Remove Success"))
}
}
// NewListTaskHandler .
func NewListTaskHandler(pecker *Pecker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "ListTaskHandler")
defer r.Body.Close()
configs, err := pecker.ListPeckTask()
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("List PeckTask failed, " + err.Error()))
return
}
stats, err := pecker.ListTaskStats()
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("List PeckTask failed, " + err.Error()))
return
}
res := make(map[string]interface{})
res["configs"] = configs
res["stats"] = stats
jsonStr, jErr := json.Marshal(res)
if jErr != nil {
panic(jErr)
}
log.Infof("[Handler] List Success: %s", jsonStr)
w.WriteHeader(http.StatusOK)
w.Write([]byte(jsonStr))
}
}
// NewTestTaskHandler .
func NewTestTaskHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "TestTaskHandler")
defer r.Body.Close()
var config PeckTaskConfig
raw, _ := ioutil.ReadAll(r.Body)
err := config.Unmarshal(raw)
if err != nil {
log.Infof("[Handler] Parse PeckTaskConfig error, %s", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Bad Request, %s in %v", err, string(raw[:]))))
return
}
results, err := TestPeckTask(&config)
if err != nil {
log.Infof("[Handler] TestTaskConfig error, %s", err)
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("test failed, " + err.Error()))
return
}
jsonStr, jErr := json.Marshal(results)
if jErr != nil {
panic(jErr)
}
log.Infof("[Handler] Test Success: %s", jsonStr)
w.WriteHeader(http.StatusOK)
w.Write(jsonStr)
return
}
}
// NewListPathHandler .
func NewListPathHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "ListPathHandler")
path := ""
if pathArr := bone.GetQuery(r, "path"); len(pathArr) > 0 {
path = pathArr[0]
} else {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Need path"))
return
}
index := strings.LastIndex(path, "/")
if index == -1 {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte("Path should be absolute"))
}
dir := path[0 : index+1]
filePrefix := path[index+1:]
files, err := ioutil.ReadDir(dir)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte(err.Error()))
return
}
var names []string
for _, file := range files {
if name := file.Name(); strings.HasPrefix(name, filePrefix) {
names = append(names, strings.TrimPrefix(name, filePrefix))
if len(names) >= 20 {
break
}
}
}
jsonStr, err := json.Marshal(names)
if err != nil {
panic(err)
}
log.Infof("[Handler] List path Success: %s", jsonStr)
w.WriteHeader(http.StatusOK)
w.Write([]byte(jsonStr))
}
}
// NewVersionHandler .
func NewVersionHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r, "VersionHandler")
w.WriteHeader(http.StatusOK)
w.Write([]byte(VersionString))
}
}