This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
/
api.go
426 lines (389 loc) · 11 KB
/
api.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package vegeta
import (
"crypto/subtle"
"net/http"
"github.com/Code-Hex/vegeta/internal/common"
"github.com/Code-Hex/vegeta/internal/model"
"github.com/Code-Hex/vegeta/internal/utils"
jwt "github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"github.com/pkg/errors"
"go.uber.org/zap"
)
const (
week uint = iota + 1
month
all
)
/* Public JSON API */
func DeleteTag() echo.HandlerFunc {
return call(func(c *Context) error {
user, ok := c.Get("user").(*model.User)
if !ok {
return errors.New("Failed to get user info via context")
}
tag := c.Param("name")
if err := user.RemoveTag(c.DB, tag); err != nil {
return c.JSON(http.StatusBadRequest, &common.ResultJSON{
Reason: err.Error(),
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
func PostTag() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(common.TagJSON)
if err := c.BindValidate(param); err != nil {
return err
}
user, ok := c.Get("user").(*model.User)
if !ok {
return errors.New("Failed to get user info via context")
}
tag := param.TagName
if err := user.AddTag(c.DB, tag); err != nil {
return c.JSON(http.StatusBadRequest, &common.ResultJSON{
Reason: err.Error(),
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
func PostData() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(common.PostDataJSON)
if err := c.BindValidate(param); err != nil {
return err
}
user, ok := c.Get("user").(*model.User)
if !ok {
return errors.New("Failed to get user info via context")
}
tag, err := user.FindByTagName(c.DB, param.TagName)
if err != nil {
return c.JSON(http.StatusBadRequest, &common.ResultJSON{
Reason: err.Error(),
})
}
data := model.Data{
RemoteAddr: param.RemoteAddr,
Payload: param.Payload,
Hostname: param.Hostname,
}
if err := tag.AddData(c.DB, data); err != nil {
return c.JSON(http.StatusBadRequest, &common.ResultJSON{
Reason: err.Error(),
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
type resultGetTagList struct {
Tags []string `json:"tags"`
}
func GetTagList() echo.HandlerFunc {
return call(func(c *Context) error {
user, ok := c.Get("user").(*model.User)
if !ok {
return errors.New("Failed to get user info via context")
}
tags := make([]string, len(user.Tags), len(user.Tags))
for i, v := range user.Tags {
tags[i] = v.Name
}
return c.JSON(http.StatusOK, &resultGetTagList{
Tags: tags,
})
})
}
type getDataList struct {
Tag string `query:"tag" validate:"required"`
Span string `query:"span" validate:"required"`
Limit uint `query:"limit" validate:"required"`
Page uint `query:"page"`
StartAt string `query:"start_at"`
EndAt string `query:"end_at"`
}
type resultGetDataList struct {
Data []model.Data `json:"data"`
}
func GetDataList() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(getDataList)
if err := c.BindValidate(param); err != nil {
return err
}
user, ok := c.Get("user").(*model.User)
if !ok {
return errors.New("Failed to get user info via context")
}
tag, err := user.FindByTagName(c.DB, param.Tag)
if err != nil {
return errors.Wrap(err, "Failed to get tag")
}
p := model.FindDataParam{
ID: tag.ID,
Page: param.Page,
Limit: param.Limit,
Span: param.Span,
StartAt: param.StartAt,
EndAt: param.EndAt,
}
data, err := model.FindDataByTagID(c.DB, p)
if err != nil {
return errors.Wrap(err, "Failed to find data")
}
return c.JSON(http.StatusOK, &resultGetDataList{
Data: data,
})
})
}
/* JSON API for settings */
type apiVegetaClaims struct {
Name string `json:"name"`
jwt.StandardClaims
}
func RegenerateToken() echo.HandlerFunc {
return call(func(c *Context) error {
token, ok := c.Get("auth_api").(*jwt.Token)
if !ok {
c.Zap.Info("Failed to check user has a permission")
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "APIトークンにユーザーの情報がありませんでした",
})
}
claim := token.Claims.(*apiVegetaClaims)
user, err := model.FindUserByName(c.DB, claim.Name)
if err != nil {
c.Zap.Info("Failed to get user at /regenerate")
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "トークンの更新に失敗しました",
})
}
if _, err := user.ReGenerateUserToken(c.DB); err != nil {
c.Zap.Info("Failed to regenerate token at /regenerate", zap.Error(err))
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "トークンの更新に失敗しました",
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
type reregisterPassword struct {
Password string `json:"password" validate:"required"`
VerifyPassword string `json:"verify_password" validate:"required"`
}
func ReRegisterPassword() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(reregisterPassword)
if err := c.BindValidate(param); err != nil {
return err
}
password := param.Password
verifyPassword := param.VerifyPassword
if subtle.ConstantTimeCompare([]byte(password), []byte(verifyPassword)) != 1 {
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "入力したパスワードと確認用のパスワードが一致しませんでした。",
})
}
token, ok := c.Get("auth_api").(*jwt.Token)
if !ok {
c.Zap.Info("Failed to check user has a permission")
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "ユーザーの情報がありませんでした",
})
}
claim := token.Claims.(*apiVegetaClaims)
user, err := model.FindUserByName(c.DB, claim.Name)
if err != nil {
c.Zap.Info("Failed to get user at /reregister_password", zap.Error(err))
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: err.Error(),
})
}
if _, err := user.UpdatePassword(c.DB, password); err != nil {
c.Zap.Info("Failed to get user at /reregister_password", zap.Error(err))
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: err.Error(),
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
/* JSON API for mypage */
type addTag struct {
Name string `json:"tag_name" validate:"required"`
}
func AddTag() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(addTag)
if err := c.BindValidate(param); err != nil {
return err
}
token, ok := c.Get("auth_api").(*jwt.Token)
if !ok {
c.Zap.Info("Failed to check user has a permission")
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "APIトークンにユーザーの情報がありませんでした",
})
}
claim := token.Claims.(*apiVegetaClaims)
user, err := model.FindUserByName(c.DB, claim.Name)
if err != nil {
c.Zap.Info("Failed to get user at /regenerate")
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "トークンの更新に失敗しました",
})
}
if err := user.AddTag(c.DB, param.Name); err != nil {
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: err.Error(),
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
type getTagsData struct {
TagID uint `json:"tag_id" validate:"required"`
Span string `json:"span" validate:"required"`
Limit uint `json:"limit" validate:"required"`
Page uint `json:"page"`
StartAt string `json:"start_at"`
EndAt string `json:"end_at"`
}
type resultGetTagsJSON struct {
IsSuccess bool `json:"is_success"`
Data []model.Data `json:"data"`
}
func JSONTagsData() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(getTagsData)
if err := c.BindValidate(param); err != nil {
return err
}
p := model.FindDataParam{
ID: param.TagID,
Page: param.Page,
Span: param.Span,
Limit: param.Limit,
StartAt: param.StartAt,
EndAt: param.EndAt,
}
data, err := model.FindDataByTagID(c.DB, p)
if err != nil {
c.Zap.Info("Failed to get tag",
zap.Error(err),
zap.Uint("tag_id", param.TagID),
)
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "データを取得するときにエラーが発生しました",
})
}
return c.JSON(http.StatusOK, &resultGetTagsJSON{
IsSuccess: true,
Data: data,
})
})
}
/* JSON API for admin */
type createUser struct {
Name string `json:"name" validate:"required"`
Password string `json:"password" validate:"required"`
VerifyPassword string `json:"verify_password" validate:"required"`
IsAdmin bool `json:"is_admin"`
}
func JSONCreateUser() echo.HandlerFunc {
return call(func(c *Context) error {
param := new(createUser)
if err := c.BindValidate(param); err != nil {
return err
}
password := param.Password
verifyPassword := param.VerifyPassword
if subtle.ConstantTimeCompare([]byte(password), []byte(verifyPassword)) != 1 {
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "入力したパスワードと確認用のパスワードが一致しませんでした。",
})
}
username := param.Name
isAdmin := param.IsAdmin
if _, err := model.CreateUser(c.DB, username, password, isAdmin); err != nil {
c.Zap.Error("Failed to create user", zap.Error(err))
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "ユーザー作成時にエラーが発生しました。",
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
type editUser struct {
ID string `json:"id" validate:"required"`
IsAdmin bool `json:"is_admin"`
IsResetPassword bool `json:"is_reset_password"`
}
func JSONEditUser() echo.HandlerFunc {
return call(func(c *Context) error {
editUser := new(editUser)
if err := c.BindValidate(editUser); err != nil {
return err
}
userID := editUser.ID
isAdmin := editUser.IsAdmin
isResetPassword := editUser.IsResetPassword
var str string
if isResetPassword {
str = utils.RandomString()
}
if _, err := model.EditUser(c.DB, userID, isAdmin, str); err != nil {
c.Zap.Error("Failed to edit user", zap.Error(err))
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "ユーザー編集時にエラーが発生しました。",
})
}
if isResetPassword {
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
Reason: str,
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}
type deleteUser struct {
ID string `json:"id" validate:"required"`
}
func JSONDeleteUser() echo.HandlerFunc {
return call(func(c *Context) error {
deleteUser := new(deleteUser)
if err := c.BindValidate(deleteUser); err != nil {
return err
}
userID := deleteUser.ID
if _, err := model.DeleteUser(c.DB, userID); err != nil {
c.Zap.Error("Failed to delete user", zap.Error(err))
return c.JSON(http.StatusOK, &common.ResultJSON{
Reason: "ユーザー削除時にエラーが発生しました。",
})
}
return c.JSON(http.StatusOK, &common.ResultJSON{
IsSuccess: true,
})
})
}