noop file and type refactor
This commit is contained in:
parent
560a4f0c57
commit
94c0dfa2a0
|
@ -69,7 +69,7 @@ func main() {
|
||||||
|
|
||||||
done := make(chan struct{}, 1)
|
done := make(chan struct{}, 1)
|
||||||
|
|
||||||
allWebhooks := make(map[string]watchdog.ConfigWebhook)
|
allWebhooks := make(map[string]watchdog.Webhook)
|
||||||
|
|
||||||
for i := range config.Webhooks {
|
for i := range config.Webhooks {
|
||||||
h := config.Webhooks[i]
|
h := config.Webhooks[i]
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,4 +1,2 @@
|
||||||
git.rootprojects.org/root/go-gitver v1.1.0 h1:ANQUnUXYgbDR+WaMcI+PQQjLnxlCbAZCD/zivkrf8fY=
|
|
||||||
git.rootprojects.org/root/go-gitver v1.1.0/go.mod h1:Rj1v3TBhvdaSphFEqMynUYwAz/4f+wY/+syBTvRrmlI=
|
|
||||||
git.rootprojects.org/root/go-gitver v1.1.1 h1:5b0lxnTYnft5hqpln0XCrJaGPH0SKzhPaazVAvAlZ8I=
|
git.rootprojects.org/root/go-gitver v1.1.1 h1:5b0lxnTYnft5hqpln0XCrJaGPH0SKzhPaazVAvAlZ8I=
|
||||||
git.rootprojects.org/root/go-gitver v1.1.1/go.mod h1:Rj1v3TBhvdaSphFEqMynUYwAz/4f+wY/+syBTvRrmlI=
|
git.rootprojects.org/root/go-gitver v1.1.1/go.mod h1:Rj1v3TBhvdaSphFEqMynUYwAz/4f+wY/+syBTvRrmlI=
|
||||||
|
|
176
watchdog.go
176
watchdog.go
|
@ -20,7 +20,7 @@ type Dog struct {
|
||||||
Keywords string
|
Keywords string
|
||||||
Recover string
|
Recover string
|
||||||
Webhooks []string
|
Webhooks []string
|
||||||
AllWebhooks map[string]ConfigWebhook
|
AllWebhooks map[string]Webhook
|
||||||
Logger chan string
|
Logger chan string
|
||||||
error error
|
error error
|
||||||
failures int
|
failures int
|
||||||
|
@ -172,94 +172,98 @@ func (d *Dog) notify(hardFail bool) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO do this in main on config init
|
d.notifyOne(h, hardFail)
|
||||||
if "" == h.Method {
|
|
||||||
h.Method = "POST"
|
|
||||||
}
|
|
||||||
|
|
||||||
var body *strings.Reader
|
|
||||||
var err error
|
|
||||||
// TODO real templates
|
|
||||||
if 0 != len(h.Form) {
|
|
||||||
form := url.Values{}
|
|
||||||
for k := range h.Form {
|
|
||||||
v := h.Form[k]
|
|
||||||
// because `{{` gets urlencoded
|
|
||||||
//k = strings.Replace(k, "{{ .Name }}", d.Name, -1)
|
|
||||||
v = strings.Replace(v, "{{ .Name }}", d.Name, -1)
|
|
||||||
d.Logger <- fmt.Sprintf("[HEADER] %s: %s", k, v)
|
|
||||||
form.Set(k, v)
|
|
||||||
}
|
|
||||||
body = strings.NewReader(form.Encode())
|
|
||||||
} else if 0 != len(h.JSON) {
|
|
||||||
bodyBuf, err := json.Marshal(h.JSON)
|
|
||||||
if nil != err {
|
|
||||||
d.Logger <- fmt.Sprintf("[Notify] JSON Marshal Error for '%s': %s", h.Name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// `{{` should be left alone
|
|
||||||
body = strings.NewReader(strings.Replace(string(bodyBuf), "{{ .Name }}", d.Name, -1))
|
|
||||||
}
|
|
||||||
|
|
||||||
client := NewHTTPClient()
|
|
||||||
req, err := http.NewRequest(h.Method, h.URL, body)
|
|
||||||
if nil != err {
|
|
||||||
d.Logger <- fmt.Sprintf("[Notify] HTTP Client Network Error for '%s': %s", h.Name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if 0 != len(h.Form) {
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
} else if 0 != len(h.JSON) {
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
|
|
||||||
if 0 != len(h.Auth) {
|
|
||||||
user := h.Auth["user"]
|
|
||||||
if "" == user {
|
|
||||||
user = h.Auth["username"]
|
|
||||||
}
|
|
||||||
pass := h.Auth["pass"]
|
|
||||||
if "" == user {
|
|
||||||
pass = h.Auth["password"]
|
|
||||||
}
|
|
||||||
req.SetBasicAuth(user, pass)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("User-Agent", "Watchdog/1.0")
|
|
||||||
for k := range h.Headers {
|
|
||||||
req.Header.Set(k, h.Headers[k])
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if nil != err {
|
|
||||||
d.Logger <- fmt.Sprintf("[Notify] HTTP Client Error for '%s': %s", h.Name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
|
|
||||||
d.Logger <- fmt.Sprintf("[Notify] Response Error for '%s': %s", h.Name, resp.Status)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO json vs xml vs txt
|
|
||||||
var data map[string]interface{}
|
|
||||||
req.Header.Add("Accept", "application/json")
|
|
||||||
decoder := json.NewDecoder(resp.Body)
|
|
||||||
err = decoder.Decode(&data)
|
|
||||||
if err != nil {
|
|
||||||
d.Logger <- fmt.Sprintf("[Notify] Response Body Error for '%s': %s", h.Name, resp.Status)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO some sort of way to determine if data is successful (keywords)
|
|
||||||
d.Logger <- fmt.Sprintf("[Notify] Success? %#v", data)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Dog) notifyOne(h Webhook, hardFail bool) {
|
||||||
|
// TODO do this in main on config init
|
||||||
|
if "" == h.Method {
|
||||||
|
h.Method = "POST"
|
||||||
|
}
|
||||||
|
|
||||||
|
var body *strings.Reader
|
||||||
|
var err error
|
||||||
|
// TODO real templates
|
||||||
|
if 0 != len(h.Form) {
|
||||||
|
form := url.Values{}
|
||||||
|
for k := range h.Form {
|
||||||
|
v := h.Form[k]
|
||||||
|
// because `{{` gets urlencoded
|
||||||
|
//k = strings.Replace(k, "{{ .Name }}", d.Name, -1)
|
||||||
|
v = strings.Replace(v, "{{ .Name }}", d.Name, -1)
|
||||||
|
d.Logger <- fmt.Sprintf("[HEADER] %s: %s", k, v)
|
||||||
|
form.Set(k, v)
|
||||||
|
}
|
||||||
|
body = strings.NewReader(form.Encode())
|
||||||
|
} else if 0 != len(h.JSON) {
|
||||||
|
bodyBuf, err := json.Marshal(h.JSON)
|
||||||
|
if nil != err {
|
||||||
|
d.Logger <- fmt.Sprintf("[Notify] JSON Marshal Error for '%s': %s", h.Name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// `{{` should be left alone
|
||||||
|
body = strings.NewReader(strings.Replace(string(bodyBuf), "{{ .Name }}", d.Name, -1))
|
||||||
|
}
|
||||||
|
|
||||||
|
client := NewHTTPClient()
|
||||||
|
req, err := http.NewRequest(h.Method, h.URL, body)
|
||||||
|
if nil != err {
|
||||||
|
d.Logger <- fmt.Sprintf("[Notify] HTTP Client Network Error for '%s': %s", h.Name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if 0 != len(h.Form) {
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
} else if 0 != len(h.JSON) {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
if 0 != len(h.Auth) {
|
||||||
|
user := h.Auth["user"]
|
||||||
|
if "" == user {
|
||||||
|
user = h.Auth["username"]
|
||||||
|
}
|
||||||
|
pass := h.Auth["pass"]
|
||||||
|
if "" == user {
|
||||||
|
pass = h.Auth["password"]
|
||||||
|
}
|
||||||
|
req.SetBasicAuth(user, pass)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("User-Agent", "Watchdog/1.0")
|
||||||
|
for k := range h.Headers {
|
||||||
|
req.Header.Set(k, h.Headers[k])
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if nil != err {
|
||||||
|
d.Logger <- fmt.Sprintf("[Notify] HTTP Client Error for '%s': %s", h.Name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
|
||||||
|
d.Logger <- fmt.Sprintf("[Notify] Response Error for '%s': %s", h.Name, resp.Status)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO json vs xml vs txt
|
||||||
|
var data map[string]interface{}
|
||||||
|
req.Header.Add("Accept", "application/json")
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
err = decoder.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
d.Logger <- fmt.Sprintf("[Notify] Response Body Error for '%s': %s", h.Name, resp.Status)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO some sort of way to determine if data is successful (keywords)
|
||||||
|
d.Logger <- fmt.Sprintf("[Notify] Success? %#v", data)
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Watches []ConfigWatch `json:"watches"`
|
Watches []ConfigWatch `json:"watches"`
|
||||||
Webhooks []ConfigWebhook `json:"webhooks"`
|
Webhooks []Webhook `json:"webhooks"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigWatch struct {
|
type ConfigWatch struct {
|
||||||
|
@ -270,7 +274,7 @@ type ConfigWatch struct {
|
||||||
RecoverScript string `json:"recover_script"`
|
RecoverScript string `json:"recover_script"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigWebhook struct {
|
type Webhook struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
|
|
Loading…
Reference in New Issue