-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
103 lines (92 loc) · 2.18 KB
/
routes.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/hashicorp/go-hclog"
"github.com/slack-go/slack"
)
const (
ApplicationJson = "application/json"
)
func NewRouter(pear *PearService) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(pear.VerifyRequest)
r.Post("/new", HandleNew(pear))
r.Post("/submit", HandleSubmit(pear))
return r
}
func WriteMsg(w http.ResponseWriter, msg slack.Msg) error {
b, err := json.Marshal(msg)
if err != nil {
return err
}
w.Header().Set("Content-Type", ApplicationJson)
_, err = w.Write(b)
return err
}
func HandleNew(pear *PearService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s, err := slack.SlashCommandParse(r)
if err != nil {
errorWrapper(w, err, http.StatusBadRequest)
return
}
msg, err := pear.HandleNew(s)
if err != nil {
errorWrapper(w, err, http.StatusInternalServerError)
return
}
if err = WriteMsg(w, msg); err != nil {
errorWrapper(w, err, http.StatusInternalServerError)
return
}
return
}
}
func ExtractInteraction(r *http.Request) (*slack.InteractionCallback, error) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
vals, err := url.ParseQuery(string(b))
if err != nil {
return nil, err
}
payload, ok := vals["payload"]
if !ok {
return nil, errors.New("missing payload")
}
var interaction slack.InteractionCallback
if err = json.Unmarshal([]byte(payload[0]), &interaction); err != nil {
return nil, fmt.Errorf("unable to unmarshal interaction: %w", err)
}
return &interaction, nil
}
func HandleSubmit(pear *PearService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
interaction, err := ExtractInteraction(r)
if err != nil {
errorWrapper(w, err, http.StatusBadRequest)
return
}
err = pear.HandleSubmit(interaction)
if err != nil {
errorWrapper(w, err, http.StatusInternalServerError)
return
}
return
}
}
func errorWrapper(w http.ResponseWriter, err error, code int) {
hclog.Default().Error(err.Error())
http.Error(w, err.Error(), code)
return
}