forked from statping-ng/statping-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
174 lines (155 loc) · 4.63 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package notifiers
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/statping-ng/statping-ng/types/failures"
"github.com/statping-ng/statping-ng/types/notifications"
"github.com/statping-ng/statping-ng/types/notifier"
"github.com/statping-ng/statping-ng/types/null"
"github.com/statping-ng/statping-ng/types/services"
"github.com/statping-ng/statping-ng/utils"
)
var _ notifier.Notifier = (*webhooker)(nil)
const (
webhookMethod = "webhook"
)
type webhooker struct {
*notifications.Notification
}
var Webhook = &webhooker{¬ifications.Notification{
Method: webhookMethod,
Title: "Webhook",
Description: "Send a custom HTTP request to a specific URL with your own body, headers, and parameters.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Icon: "fas fa-code-branch",
Delay: time.Duration(3 * time.Second),
SuccessData: null.NewNullString(`{"id": "{{.Service.Id}}", "online": true}`),
FailureData: null.NewNullString(`{"id": "{{.Service.Id}}", "online": false}`),
DataType: "json",
Limits: 180,
Form: []notifications.NotificationForm{{
Type: "text",
Title: "HTTP Endpoint",
Placeholder: "http://webhookurl.com/JW2MCP4SKQP",
SmallText: "Insert the URL for your HTTP Requests.",
DbField: "Host",
Required: true,
}, {
Type: "list",
Title: "HTTP Method",
Placeholder: "POST",
SmallText: "Choose a HTTP method for example: GET, POST, DELETE, or PATCH.",
DbField: "Var1",
Required: true,
ListOptions: []string{"GET", "POST", "PATCH", "DELETE"},
}, {
Type: "text",
Title: "Content Type",
Placeholder: `application/json`,
SmallText: "Optional content type for example: application/json or text/plain",
DbField: "api_key",
}, {
Type: "text",
Title: "Header",
Placeholder: "Authorization=Token12345",
SmallText: "Optional Headers for request use format: KEY=Value,Key=Value",
DbField: "api_secret",
},
}}}
// Send will send a HTTP Post to the webhooker API. It accepts type: string
func (w *webhooker) Send(msg interface{}) error {
resp, err := w.sendHttpWebhook(msg.(string))
if err == nil {
resp.Body.Close()
}
return err
}
func (w *webhooker) Select() *notifications.Notification {
return w.Notification
}
func (w *webhooker) Valid(values notifications.Values) error {
return nil
}
func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) {
client := new(http.Client)
client.Timeout = 10 * time.Second
req, err := http.NewRequest(w.Var1.String, w.Host.String, bytes.NewBufferString(body))
if err != nil {
return nil, err
}
if w.ApiKey.String != "" {
req.Header.Add("Content-Type", w.ApiKey.String)
} else {
req.Header.Add("Content-Type", "application/json")
}
req.Header.Set("User-Agent", "Statping-ng")
req.Header.Set("Statping-Version", utils.Params.GetString("VERSION"))
var customHeaders []string
if w.ApiSecret.String != "" {
customHeaders = strings.Split(w.ApiSecret.String, ",")
} else {
customHeaders = nil
}
for _, h := range customHeaders {
keyVal := strings.SplitN(h, "=", 2)
if len(keyVal) == 2 {
if keyVal[0] != "" && keyVal[1] != "" {
if strings.ToLower(keyVal[0]) == "host" {
req.Host = strings.TrimSpace(keyVal[1])
} else {
req.Header.Set(keyVal[0], keyVal[1])
}
}
}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, err
}
func (w *webhooker) OnTest() (string, error) {
f := failures.Example()
s := services.Example(false)
body := ReplaceVars(w.SuccessData.String, s, f)
resp, err := w.sendHttpWebhook(body)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
out := fmt.Sprintf("Webhook notifier received: '%v'", string(content))
utils.Log.Infoln(out)
return out, err
}
// OnFailure will trigger failing service
func (w *webhooker) OnFailure(s services.Service, f failures.Failure) (string, error) {
msg := ReplaceVars(w.FailureData.String, s, f)
resp, err := w.sendHttpWebhook(msg)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
return string(content), err
}
// OnSuccess will trigger successful service
func (w *webhooker) OnSuccess(s services.Service) (string, error) {
msg := ReplaceVars(w.SuccessData.String, s, failures.Failure{})
resp, err := w.sendHttpWebhook(msg)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
return string(content), err
}
// OnSave will trigger when this notifier is saved
func (w *webhooker) OnSave() (string, error) {
return "", nil
}