You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.8 KiB
86 lines
1.8 KiB
5 years ago
|
package webhooks
|
||
5 years ago
|
|
||
|
import (
|
||
5 years ago
|
"bytes"
|
||
|
"encoding/json"
|
||
5 years ago
|
"io/ioutil"
|
||
5 years ago
|
"net/http"
|
||
5 years ago
|
|
||
|
"gopkg.in/yaml.v2"
|
||
|
)
|
||
|
|
||
5 years ago
|
const (
|
||
|
// DefaultWebHookPath ..
|
||
5 years ago
|
DefaultWebHookPath = "webhooks/webhook.example.yaml"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
// AvailabilityHooks ..
|
||
|
type AvailabilityHooks struct {
|
||
5 years ago
|
OnDroppedBelowThreshold string `yaml:"on-dropped-below-threshold"`
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// DoubleSignWebHooks ..
|
||
|
type DoubleSignWebHooks struct {
|
||
5 years ago
|
OnNoticeDoubleSign string `yaml:"on-notice-double-sign"`
|
||
|
}
|
||
|
|
||
|
// BadBlockHooks ..
|
||
|
type BadBlockHooks struct {
|
||
|
OnCannotCommit string `yaml:"on-cannot-commit-block"`
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// Hooks ..
|
||
|
type Hooks struct {
|
||
5 years ago
|
Slashing *DoubleSignWebHooks `yaml:"slashing-hooks"`
|
||
|
Availability *AvailabilityHooks `yaml:"availability-hooks"`
|
||
|
ProtocolIssues *BadBlockHooks `yaml:"protocol-hooks"`
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
// ReportResult ..
|
||
|
type ReportResult struct {
|
||
|
Result string `json:"result"`
|
||
|
Payload string `json:"payload"`
|
||
|
}
|
||
|
|
||
|
// NewSuccess ..
|
||
|
func NewSuccess(payload string) *ReportResult {
|
||
|
return &ReportResult{"success", payload}
|
||
|
}
|
||
|
|
||
|
// NewFailure ..
|
||
|
func NewFailure(payload string) *ReportResult {
|
||
|
return &ReportResult{"failure", payload}
|
||
|
}
|
||
|
|
||
|
// DoPost is a fire and forget helper
|
||
5 years ago
|
func DoPost(url string, record interface{}) (*ReportResult, error) {
|
||
5 years ago
|
payload, err := json.Marshal(record)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
result, err := ioutil.ReadAll(resp.Body)
|
||
|
anon := ReportResult{}
|
||
|
if err := json.Unmarshal(result, &anon); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &anon, nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// NewWebHooksFromPath ..
|
||
|
func NewWebHooksFromPath(yamlPath string) (*Hooks, error) {
|
||
|
rawYAML, err := ioutil.ReadFile(yamlPath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
t := Hooks{}
|
||
|
if err := yaml.UnmarshalStrict(rawYAML, &t); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &t, nil
|
||
|
}
|