forked from topfreegames/khan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.go
129 lines (110 loc) · 2.92 KB
/
hook.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
// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
package api
import (
"net/http"
"time"
"github.com/labstack/echo"
"github.com/topfreegames/khan/log"
"github.com/topfreegames/khan/models"
"github.com/uber-go/zap"
)
//CreateHookHandler is the handler responsible for creating new hooks
func CreateHookHandler(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
c.Set("route", "CreateHook")
start := time.Now()
gameID := c.Param("gameID")
db := app.Db(c.StdContext())
l := app.Logger.With(
zap.String("source", "CreateHookHandler"),
zap.String("operation", "createHook"),
zap.String("gameID", gameID),
)
var payload HookPayload
err := WithSegment("payload", c, func() error {
if err := LoadJSONPayload(&payload, c, l); err != nil {
log.E(l, "Failed to parse json payload.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return err
}
return nil
})
if err != nil {
return FailWith(http.StatusBadRequest, err.Error(), c)
}
var hook *models.Hook
err = WithSegment("hook-create", c, func() error {
log.D(l, "Creating hook...")
hook, err = models.CreateHook(
db,
gameID,
payload.Type,
payload.HookURL,
)
if err != nil {
log.E(l, "Failed to create the hook.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return err
}
return nil
})
if err != nil {
return FailWith(http.StatusInternalServerError, err.Error(), c)
}
log.I(l, "Created hook successfully.", func(cm log.CM) {
cm.Write(
zap.String("hookPublicID", hook.PublicID),
zap.Duration("duration", time.Now().Sub(start)),
)
})
return SucceedWith(map[string]interface{}{
"publicID": hook.PublicID,
}, c)
}
}
// RemoveHookHandler is the handler responsible for removing existing hooks
func RemoveHookHandler(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
c.Set("route", "RemoveHook")
start := time.Now()
gameID := c.Param("gameID")
publicID := c.Param("publicID")
db := app.Db(c.StdContext())
l := app.Logger.With(
zap.String("source", "RemoveHookHandler"),
zap.String("operation", "removeHook"),
zap.String("gameID", gameID),
zap.String("hookPublicID", publicID),
)
var err error
err = WithSegment("hook-remove", c, func() error {
log.D(l, "Removing hook...")
err = models.RemoveHook(
db,
gameID,
publicID,
)
if err != nil {
log.E(l, "Failed to remove hook.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return err
}
return nil
})
if err != nil {
return FailWith(http.StatusInternalServerError, err.Error(), c)
}
log.I(l, "Hook removed successfully.", func(cm log.CM) {
cm.Write(zap.Duration("duration", time.Now().Sub(start)))
})
return SucceedWith(map[string]interface{}{}, c)
}
}