-
Notifications
You must be signed in to change notification settings - Fork 3
/
webhook.go
45 lines (36 loc) · 847 Bytes
/
webhook.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
package inputs
import (
"fmt"
"github.com/hexbotio/hex/models"
"io/ioutil"
"log"
"net/http"
"strconv"
)
type Webhook struct {
}
func (x Webhook) Read(inputMsgs chan<- models.Message, service models.Service) {
defer Recovery(service)
port, _ := strconv.Atoi(service.Config["Port"])
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: nil,
}
handle := func(w http.ResponseWriter, r *http.Request) {
rawbody, err := ioutil.ReadAll(r.Body)
body := string(rawbody)
if err != nil {
log.Print(err)
}
defer r.Body.Close()
message := models.MakeMessage(service.Type, service.Name, r.RequestURI, r.RemoteAddr, body)
inputMsgs <- message
w.WriteHeader(http.StatusOK)
w.Write([]byte("HexBot"))
}
http.HandleFunc("/", handle)
err := server.ListenAndServe()
if err != nil {
log.Print(err)
}
}