-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
195 lines (151 loc) Β· 3.48 KB
/
handlers.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
package main
import (
"bytes"
"fmt"
"net/http"
"text/template"
log "github.com/Sirupsen/logrus"
"github.com/labstack/echo"
)
func UserListHandler(c echo.Context) error {
users, err := ListUser()
if err != nil {
log.Warn(err)
return err
}
collection := make(map[string]interface{})
collection["data"] = users
return c.JSON(http.StatusOK, collection)
}
func AddUserHandler(c echo.Context) error {
u := new(User)
u.Always = false // By default, do not send SMS when no TimeSlot
if err := c.Bind(u); err != nil {
log.Warn(err)
return err
}
if err := AddUser(u); err != nil {
log.Warn(err)
return err
}
log.WithFields(log.Fields{
"user": u.ID,
}).Info("New user added")
return c.JSON(http.StatusCreated, u)
}
func RemoveUserHandler(c echo.Context) error {
ID := c.Param("id")
if err := RemoveUser(ID); err != nil {
log.Warn(err)
return err
}
log.WithFields(log.Fields{
"user": ID,
}).Info("User removed")
return c.NoContent(http.StatusNoContent)
}
func WebhookListHandler(c echo.Context) error {
webhooks, err := ListWebhook()
if err != nil {
log.Warn(err)
return err
}
collection := make(map[string]interface{})
collection["data"] = webhooks
return c.JSON(http.StatusOK, collection)
}
func AddWebhookHandler(c echo.Context) error {
w := new(Webhook)
if err := c.Bind(w); err != nil {
log.Warn(err)
return err
}
if err := AddWebhook(w); err != nil {
log.Warn(err)
return err
}
log.WithFields(log.Fields{
"webhook": w.ID,
}).Info("New webhook added")
return c.JSON(http.StatusCreated, w)
}
func RemoveWebhookHandler(c echo.Context) error {
ID := c.Param("id")
if err := RemoveWebhook(ID); err != nil {
log.Warn(err)
return err
}
log.WithFields(log.Fields{
"webhook": ID,
}).Info("Webhook removed")
return c.NoContent(http.StatusNoContent)
}
func WebhookHandler(c echo.Context) error {
name := c.Param("name")
w, err := GetWebhookByName(name)
if err != nil {
log.Warn(err)
return echo.NewHTTPError(http.StatusNotFound)
}
t := template.Must(template.New(name).Parse(w.Template))
var data map[string]interface{}
ct := c.Request().Header[echo.HeaderContentType]
switch ct[0] {
case echo.MIMEApplicationForm, echo.MIMEMultipartForm:
data = make(map[string]interface{})
params, _ := c.FormParams()
for key, value := range params {
if len(value) == 1 {
data[key] = value[0]
} else {
data[key] = value
}
}
default:
if err := c.Bind(&data); err != nil {
log.Warn(err)
return echo.NewHTTPError(http.StatusBadRequest)
}
}
var b bytes.Buffer
err = t.Execute(&b, data)
if err != nil {
log.Warn(err)
return err
}
message := b.String()
message = fmt.Sprintf("%s:\n%s", name, message)
slots, err := GetCurrentTimeSlot()
if err != nil {
log.Warn(err)
return err
}
users, err := GetAlwaysUser()
if err != nil {
log.Warn(err)
return err
}
receivers := make(map[string]*User)
// execute for user on duty
for _, ts := range slots {
receivers[ts.User.ID] = ts.User
}
// only send to always if nobody's on duty
if len(receivers) == 0 {
// execute for user with always on
for _, u := range users {
receivers[u.ID] = u
}
}
// loop over receivers to prevent recipient message duplication
for _, u := range receivers {
event := NewEvent(u, w, message, c.Request(), c.RealIP())
AddEvent(event)
log.WithFields(log.Fields{
"user": u.Name,
"webhook": name,
}).Info("Webhook triggered")
tc.SensSMS(u.Number, message)
}
return c.NoContent(http.StatusCreated)
}