-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
60 lines (52 loc) · 1.23 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
package main
import (
"net/http"
"os/exec"
"github.com/gin-gonic/gin"
"github.com/inconshreveable/log15"
)
func cowsayHandler(cowsayExec string, allowed map[string]interface{}, log log15.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
if len(allowed) > 0 {
log.Info(
"Processing tokens",
"Count", len(allowed),
)
token, _ := c.GetPostForm("token")
_, ok := allowed[token]
if !ok {
log.Error(
"Token Rejected",
"Token", token,
)
c.AbortWithStatus(http.StatusForbidden)
return
}
}
text, _ := c.GetPostForm("text")
out, err := exec.Command(cowsayExec, string(text)).Output()
if err != nil {
log.Error(
"Cowsay not found",
"Error", err,
"Location", cowsayExec,
)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
codeMark := []byte("```")
out = append(codeMark, out...)
out = append(out, codeMark...)
back := CowsayResponse{
Response_type: "in_channel",
Text: string(out),
}
c.Header("content-type", "application/json")
c.JSON(http.StatusOK, back)
}
}
type CowsayResponse struct {
Response_type string `json:"response_type"`
Text string `json:"text"`
Attachments []string `json:"attachments"`
}