-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
65 lines (53 loc) · 1.37 KB
/
server.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
package slackcmd
import (
"fmt"
"io"
"log"
"net/http"
"strings"
)
// Handler is a http.Handler function that can be added to http server to handle Slack Commands
func Handler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
contactAdminMsg(w)
return
}
payload := newPayloadByForm(r.Form)
if !payload.IsValid() {
contactAdminMsg(w)
return
}
cmd, err := getCommander(payload.Command)
if err != nil {
io.WriteString(w, fmt.Sprintf("Command %q was not found", payload.Command))
return
}
var validator PayloadValidator
if _, ok := cmd.(PayloadValidator); ok {
validator = cmd.(PayloadValidator)
} else {
validator = NewTokenValidator()
}
if err := validator.Validate(payload); err != nil {
contactAdminMsg(w)
return
}
if err := cmd.Execute(payload, w); err != nil {
io.WriteString(w, err.Error())
}
}
func contactAdminMsg(w io.Writer) {
io.WriteString(w, "Something goes wrong, please contact your administrator.")
}
// StartServer start a server which will receive and process Slack command
func StartServer(host, port, path string) {
if strings.TrimSpace(path) == "" {
path = "/"
}
http.HandleFunc(path, Handler)
address := host + ":" + port
log.Printf("Server is listening at %s%s", address, path)
if err := http.ListenAndServe(address, nil); err != nil {
log.Fatalf("Could not start server: %v", err)
}
}