-
Notifications
You must be signed in to change notification settings - Fork 307
/
webhook.go
101 lines (87 loc) · 1.84 KB
/
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
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
package commands
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"time"
"github.com/urfave/cli/v2"
)
func init() {
DefaultList = append(DefaultList, WEBHOOK())
}
func WEBHOOK() *cli.Command {
c := &cli.Command{
Name: "webhook",
Usage: "spin up a webhook destination server",
Subcommands: []*cli.Command{
{
Name: "run",
Usage: "switch between normal and degraded mode of rudder-server",
Action: WebhookRun,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Usage: "specify the port to listen on",
Value: 8083,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "print more",
Value: false,
},
},
},
},
}
return c
}
func WebhookRun(c *cli.Context) error {
port := c.Int("port")
fmt.Printf("listening on: http://localhost:%d \n", port)
httpWebServer := &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: &webhook{
Verbose: c.Bool("verbose"),
},
ReadTimeout: 0 * time.Second,
ReadHeaderTimeout: 0 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 720 * time.Second,
MaxHeaderBytes: 524288,
}
return httpWebServer.ListenAndServe()
}
type webhook struct {
Verbose bool
}
type payload struct {
SentAt string
}
func (*webhook) computeTime(b []byte) {
p := payload{}
err := json.Unmarshal(b, &p)
if err != nil {
return
}
sentAt, err := time.Parse(time.RFC3339, p.SentAt)
if err != nil {
log.Println(err)
return
}
log.Println("got event after:", time.Since(sentAt))
}
func (wh *webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
wh.computeTime(b)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}