-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
325 lines (280 loc) · 9.42 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/ECSTeam/labcheck/db"
"github.com/ECSTeam/labcheck/helpers"
"github.com/ECSTeam/labcheck/model"
)
func index(w http.ResponseWriter, r *http.Request) {
Render.JSON(w, http.StatusOK, map[string]string{"welcome": "This is rendered JSON!"})
}
//LabsHandler handler for GET on /labs
func labsHandler(w http.ResponseWriter, r *http.Request) {
l, err := db.DB.ListLabs()
if err != nil {
log.Printf("could not list labs: %v", err)
}
Render.JSON(w, http.StatusOK, l)
}
//LoadData handler for POST /load
func loadData(w http.ResponseWriter, r *http.Request) {
var labs []model.Lab
defer r.Body.Close()
payload, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err := json.Unmarshal(payload, &labs); err != nil {
Render.JSON(w, http.StatusUnprocessableEntity, err)
}
if err := db.DB.LoadLabs(labs); err != nil {
log.Printf("error loading labs...%v", err)
}
if err := Render.JSON(w, http.StatusCreated, map[string]string{"Success": "Labs created successfully!"}); err != nil {
panic(err)
}
}
//LabCheck handler for POST /labs
func labCheck(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
Render.JSON(w, http.StatusUnprocessableEntity, err)
}
defer r.Body.Close()
if os.Getenv("LOCAL") == "false" {
if helpers.CheckToken(r.FormValue("token")) == false {
//put call to token helper here
Render.JSON(w, http.StatusUnauthorized, "invalid token")
}
}
var slack = initSlackRequest(r)
var slackResponse SlackResponse
var comment = ""
var rgx = regexp.MustCompile(`\{(.*?)\}`)
//empty texts returns all labs
if len(strings.TrimSpace(slack.Text)) == 0 {
l, err := db.DB.ListLabs()
if err != nil {
log.Printf("could not list labs: %v", err)
}
slackResponse.ResponseType = "ephemeral"
slackResponse.Text = "Labs"
slackResponse.Attachments = make([]Attachments, 0)
for _, lab := range l {
attachment := Attachments{
Pretext: lab.Name,
Color: color(lab.Available),
Text: "version: " + lab.Version + " Description:" + lab.Desc,
Footer: lab.User,
Timestamp: lab.LastUpdate.Unix(),
}
slackResponse.Attachments = append(slackResponse.Attachments, attachment)
}
Render.JSON(w, http.StatusOK, slackResponse)
return
}
//labs help command
if slack.Text == "help" {
slackResponse.ResponseType = "ephemeral"
slackResponse.Text = "Labcheck Help"
slackResponse.Attachments = make([]Attachments, 0)
attachment := Attachments{
Title: "Labcheck README.md page",
TitleLink: "https://github.com/ECSTeam/labcheck",
Text: `Commands:
/labs
/labs checkout labxx {"_optional comment_"}
/labs checkin labxx
/labs status labxx
/labs update labxx {"version":"x.x", "desc":"..."}
/labs help`,
}
slackResponse.Attachments = append(slackResponse.Attachments, attachment)
Render.JSON(w, http.StatusOK, slackResponse)
return
}
stringSlice := strings.Split(slack.Text, " ")
//if text starts with check - do some checkin/checkout logic
if strings.Contains(strings.ToLower(stringSlice[0]), "check") {
checkStatus := stringSlice[0]
//get lab by name which should be the 2nd string
lab, err := db.DB.GetLabByName(stringSlice[1])
//entity not found, return a 204
if err != nil {
Render.JSON(w, http.StatusNoContent, err)
return
}
lab.LastUpdate = time.Now()
if strings.Compare(checkStatus, "checkin") == 0 {
lab.Available = true
lab.User = ""
}
if strings.Compare(checkStatus, "checkout") == 0 {
lab.Available = false
lab.User = slack.User
var cmt = rgx.FindStringSubmatch(slack.Text)
if len(cmt) > 0 {
comment = cmt[1]
}
}
db.DB.UpdateLab(lab)
slackResponse.ResponseType = "in_channel"
slackResponse.Text = lab.Name
slackResponse.Attachments = make([]Attachments, 0)
attachment := Attachments{
Color: color(lab.Available),
Title: checkStatus,
AuthorName: slack.User,
Text: comment,
}
slackResponse.Attachments = append(slackResponse.Attachments, attachment)
Render.JSON(w, http.StatusOK, slackResponse)
return
}
//labs status <labname>
if strings.Contains(strings.ToLower(stringSlice[0]), "status") {
var labName = strings.ToLower(stringSlice[1])
lab, err := db.DB.GetLabByName(labName)
if err != nil {
Render.JSON(w, http.StatusNotFound, labName)
return
}
slackResponse.Text = lab.Name
slackResponse.Attachments = make([]Attachments, 0)
attachment := Attachments{
Color: color(lab.Available),
AuthorName: lab.User,
Text: "version: " + lab.Version + " | Description: " + lab.Desc,
Timestamp: lab.LastUpdate.Unix(),
}
slackResponse.Attachments = append(slackResponse.Attachments, attachment)
Render.JSON(w, http.StatusOK, slackResponse)
return
}
//labs update <labname>
if strings.Contains(strings.ToLower(stringSlice[0]), "update") {
var labName = strings.ToLower(stringSlice[1])
var regexString = rgx.FindStringSubmatch(slack.Text)
var l model.Lab
b := []byte("{" + regexString[1] + "}")
err := json.Unmarshal(b, &l)
if err != nil {
Render.JSON(w, http.StatusNotModified, err)
return
}
lab, err := db.DB.GetLabByName(labName)
if err != nil {
Render.JSON(w, http.StatusNotFound, labName)
return
}
lab.LastUpdate = time.Now()
lab.Desc = l.Desc
lab.Version = l.Version
db.DB.UpdateLab(lab)
slackResponse.Text = lab.Name
slackResponse.Attachments = make([]Attachments, 0)
attachment := Attachments{
Color: color(lab.Available),
AuthorName: lab.User,
Text: "version: " + lab.Version + " | Description: " + lab.Desc,
Timestamp: lab.LastUpdate.Unix(),
}
slackResponse.Attachments = append(slackResponse.Attachments, attachment)
if strings.Compare(slack.Text, slack.OriginalText) != 0 {
attachment := Attachments{
Text: "Original text contained special, incompatible, characters. \n*Original text*: " + slack.OriginalText + "\n*Cleansed text*: " + slack.Text,
}
slackResponse.Attachments = append(slackResponse.Attachments, attachment)
}
Render.JSON(w, http.StatusOK, slackResponse)
if err != nil {
// handle the error, often:
fmt.Println(err)
}
//HTML doesn't render in slack...but if it did!!!
//Render.HTML(w, http.StatusOK, "labs", lab)
}
}
func color(c bool) string {
if c {
return "#000000"
}
return "#ff0000"
}
func initSlackRequest(r *http.Request) model.Slack {
var slack model.Slack
slack.Command = r.FormValue("command")
slack.User = r.FormValue("user_name")
slack.OriginalText = r.FormValue("text")
slack.Text = cleanupSmartQuotes(slack.OriginalText)
slack.ResponseURL = r.FormValue("response_url")
slack.TeamDomain = r.FormValue("team_domain")
slack.ChannelID = r.FormValue("channel_id")
slack.ChannelName = r.FormValue("channel_name")
slack.UserID = r.FormValue("user_id")
slack.Token = r.FormValue("token")
slack.TeamID = r.FormValue("team_id")
return slack
}
func cleanupSmartQuotes(myText string) string {
returnText := myText
returnText = strings.Replace(returnText, "\uFFFD", `\\'`, -1)
returnText = strings.Replace(returnText, "\u201A", `\\'`, -1)
returnText = strings.Replace(returnText, "\u2018", `\\'`, -1)
returnText = strings.Replace(returnText, "“", `"`, -1)
returnText = strings.Replace(returnText, "”", `"`, -1)
returnText = strings.Replace(returnText, "’", `'`, -1)
returnText = strings.Replace(returnText, "\u2019", `\\'`, -1)
returnText = strings.Replace(returnText, "\u0146", `\\'`, -1)
returnText = strings.Replace(returnText, "\u0147", `\\"`, -1)
returnText = strings.Replace(returnText, "\u0148", `\\"`, -1)
returnText = strings.Replace(returnText, "\u201c", `\\"`, -1)
returnText = strings.Replace(returnText, "\u201d", `\\"`, -1)
returnText = strings.Replace(returnText, "\u201e", `\\"`, -1)
returnText = strings.Replace(returnText, "\u02C6", `^`, -1)
returnText = strings.Replace(returnText, "\u2039", `<`, -1)
returnText = strings.Replace(returnText, "\u203A", `>`, -1)
returnText = strings.Replace(returnText, "\u2013", `-`, -1)
returnText = strings.Replace(returnText, "\u2014", `--`, -1)
returnText = strings.Replace(returnText, "\u2026", `...`, -1)
returnText = strings.Replace(returnText, "\u00A9", `(c)`, -1)
returnText = strings.Replace(returnText, "\u00AE", `(r)`, -1)
returnText = strings.Replace(returnText, "\u2122", `TM`, -1)
returnText = strings.Replace(returnText, "\u00BC", `1/4`, -1)
returnText = strings.Replace(returnText, "\u00BD", `1/2`, -1)
returnText = strings.Replace(returnText, "\u00BE", `3/4`, -1)
returnText = strings.Replace(returnText, "\u02DC", ` `, -1)
returnText = strings.Replace(returnText, "\u00A0", ` `, -1)
return returnText
}
//SlackResponse represents a slack response
type SlackResponse struct {
ResponseType string `json:"response_type"`
Text string `json:"text"`
Attachments []Attachments `json:"attachments"`
}
type Attachments struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Pretext string `json:"pretext"`
AuthorName string `json:"author_name"`
AuthorLink string `json:"author_link"`
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
ImageURL string `json:"image_url"`
ThumbURL string `json:"thumb_url"`
Footer string `json:"footer"`
FooterIcon string `json:"footer_icon"`
Timestamp int64 `json:"ts"`
}
type Fields struct {
Title string `json:"title"`
Value string `json:"value"`
Short string `json:"short"`
}