http webhook notifier - checkins

pull/78/head
Hunter Long 2018-10-03 01:17:25 -07:00
parent baf6ca3d34
commit ba85a96ab8
9 changed files with 311 additions and 17 deletions

View File

@ -291,7 +291,7 @@ func (db *DbConfig) DropDatabase() error {
func (db *DbConfig) CreateDatabase() error {
utils.Log(1, "Creating Database Tables...")
err := DbSession.CreateTable(&types.Checkin{})
//err = DbSession.CreateTable(&types.CheckinHit{})
err = DbSession.CreateTable(&types.CheckinHit{})
err = DbSession.CreateTable(&notifier.Notification{})
err = DbSession.Table("core").CreateTable(&types.Core{})
err = DbSession.CreateTable(&types.Failure{})
@ -317,7 +317,7 @@ func (db *DbConfig) MigrateDatabase() error {
if tx.Error != nil {
return tx.Error
}
tx = tx.AutoMigrate(&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Checkin{}, &notifier.Notification{}).Table("core").AutoMigrate(&types.Core{})
tx = tx.AutoMigrate(&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Checkin{}, &types.CheckinHit{}, &notifier.Notification{}).Table("core").AutoMigrate(&types.Core{})
if tx.Error != nil {
tx.Rollback()
utils.Log(3, fmt.Sprintf("Statup Database could not be migrated: %v", tx.Error))

View File

@ -51,7 +51,7 @@ func SelectService(id int64) *Service {
func (s *Service) Checkins() []*types.Checkin {
var hits []*types.Checkin
servicesDB().Where("service = ?", s.Id).Scan(&hits)
servicesDB().Where("service = ?", s.Id).Find(&hits)
return hits
}

View File

@ -89,8 +89,8 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap
"js": func(html interface{}) template.JS {
return template.JS(utils.ToString(html))
},
"safe": func(html interface{}) template.HTML {
return template.HTML(utils.ToString(html))
"safe": func(html string) template.HTML {
return template.HTML(html)
},
"Auth": func() bool {
return IsAuthenticated(r)

View File

@ -47,7 +47,6 @@ func Router() *mux.Router {
r.PathPrefix("/statup.png").Handler(http.FileServer(source.TmplBox.HTTPBox()))
}
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(source.JsBox.HTTPBox())))
//r.Handle("/charts/{id}.js", http.HandlerFunc(renderServiceChartHandler))
r.Handle("/charts.js", http.HandlerFunc(renderServiceChartsHandler))
r.Handle("/setup", http.HandlerFunc(setupHandler)).Methods("GET")
r.Handle("/setup", http.HandlerFunc(processSetupHandler)).Methods("POST")

185
notifiers/webhook.go Normal file
View File

@ -0,0 +1,185 @@
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package notifiers
import (
"bytes"
"fmt"
"github.com/hunterlong/statup/core/notifier"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"io/ioutil"
"net/http"
"strings"
"time"
)
const (
WEBHOOK_METHOD = "webhook"
)
type Webhook struct {
*notifier.Notification
}
var webhook = &Webhook{&notifier.Notification{
Method: WEBHOOK_METHOD,
Title: "HTTP 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",
Delay: time.Duration(1 * time.Second),
Form: []notifier.NotificationForm{{
Type: "text",
Title: "HTTP Endpoint",
Placeholder: "http://webhookurl.com/JW2MCP4SKQP",
SmallText: "Insert the URL for your HTTP Requests",
DbField: "Host",
Required: true,
}, {
Type: "text",
Title: "HTTP Method",
Placeholder: "POST",
SmallText: "Choose a HTTP method for example: GET, POST, DELETE, or PATCH",
DbField: "Var1",
Required: true,
}, {
Type: "textarea",
Title: "HTTP Body",
Placeholder: `{"service_id": "%s.Id", "service_name": "%s.Name"}`,
SmallText: "Optional HTTP body for a POST request. You can insert variables into your body request.<br>%service.Id, %service.Name<br>%failure.Issue",
DbField: "Var2",
}, {
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",
},
}}}
// DEFINE YOUR NOTIFICATION HERE.
func init() {
err := notifier.AddNotifier(webhook)
if err != nil {
panic(err)
}
}
// Send will send a HTTP Post to the Webhook API. It accepts type: string
func (w *Webhook) Send(msg interface{}) error {
message := msg.(string)
_, err := w.run(message)
return err
}
func (w *Webhook) Select() *notifier.Notification {
return w.Notification
}
func replaceBodyText(body string, s *types.Service, f *types.Failure) string {
if s != nil {
body = strings.Replace(body, "%service.Name", s.Name, -1)
body = strings.Replace(body, "%service.Id", utils.ToString(s.Id), -1)
}
if f != nil {
body = strings.Replace(body, "%failure.Issue", f.Issue, -1)
}
return body
}
func (w *Webhook) run(body string) (*http.Response, error) {
utils.Log(1, fmt.Sprintf("sending body: '%v' to %v as a %v request", body, w.Host, w.Var1))
client := new(http.Client)
client.Timeout = time.Duration(10 * time.Second)
var buf *bytes.Buffer
buf = bytes.NewBuffer(nil)
if w.Var2 != "" {
buf = bytes.NewBuffer([]byte(w.Var2))
}
req, err := http.NewRequest(w.Var1, w.Host, buf)
if err != nil {
return nil, err
}
if w.ApiSecret != "" {
splitArray := strings.Split(w.ApiSecret, ",")
for _, a := range splitArray {
split := strings.Split(a, "=")
req.Header.Add(split[0], split[1])
}
}
if w.ApiSecret != "" {
req.Header.Add("Content-Type", w.ApiSecret)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, err
}
func (w *Webhook) OnTest() error {
service := &types.Service{
Id: 1,
Name: "Interpol - All The Rage Back Home",
Domain: "https://www.youtube.com/watch?v=-u6DvRyyKGU",
ExpectedStatus: 200,
Interval: 30,
Type: "http",
Method: "GET",
Timeout: 20,
LastStatusCode: 404,
Expected: "test example",
LastResponse: "<html>this is an example response</html>",
CreatedAt: time.Now().Add(-24 * time.Hour),
}
body := replaceBodyText(w.Var2, service, nil)
resp, err := w.run(body)
if err != nil {
return err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
utils.Log(1, fmt.Sprintf("webhook notifier received: '%v'", string(content)))
return err
}
// OnFailure will trigger failing service
func (w *Webhook) OnFailure(s *types.Service, f *types.Failure) {
msg := replaceBodyText(w.Var2, s, f)
webhook.AddQueue(msg)
w.Online = false
}
// OnSuccess will trigger successful service
func (w *Webhook) OnSuccess(s *types.Service) {
if !w.Online {
msg := replaceBodyText(w.Var2, s, nil)
webhook.AddQueue(msg)
}
w.Online = true
}
// OnSave triggers when this notifier has been saved
func (w *Webhook) OnSave() error {
return nil
}

104
notifiers/webhook_test.go Normal file
View File

@ -0,0 +1,104 @@
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package notifiers
import (
"github.com/hunterlong/statup/core/notifier"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var (
WEBHOOK_URL = "https://jsonplaceholder.typicode.com/posts"
webhookMessage = `{ "title": "%service.Id", "body": "%service.Name", "userId": 19999 }`
fullMsg string
)
func init() {
webhook.Host = WEBHOOK_URL
webhook.Var1 = "POST"
}
func TestWebhookNotifier(t *testing.T) {
t.Parallel()
currentCount = CountNotifiers()
t.Run("Load Webhook", func(t *testing.T) {
webhook.Host = WEBHOOK_URL
webhook.Delay = time.Duration(100 * time.Millisecond)
err := notifier.AddNotifier(webhook)
assert.Nil(t, err)
assert.Equal(t, "Hunter Long", webhook.Author)
assert.Equal(t, WEBHOOK_URL, webhook.Host)
})
t.Run("Load Webhook Notifier", func(t *testing.T) {
notifier.Load()
})
t.Run("Webhook Notifier Tester", func(t *testing.T) {
assert.True(t, webhook.CanTest())
})
t.Run("Webhook Replace Body Text", func(t *testing.T) {
fullMsg = replaceBodyText(webhookMessage, TestService, TestFailure)
assert.Equal(t, 78, len(fullMsg))
})
t.Run("Webhook Within Limits", func(t *testing.T) {
ok, err := webhook.WithinLimits()
assert.Nil(t, err)
assert.True(t, ok)
})
t.Run("Webhook OnFailure", func(t *testing.T) {
webhook.OnFailure(TestService, TestFailure)
assert.Len(t, webhook.Queue, 1)
})
t.Run("Webhook Check Offline", func(t *testing.T) {
assert.False(t, webhook.Online)
})
t.Run("Webhook OnSuccess", func(t *testing.T) {
webhook.OnSuccess(TestService)
assert.Len(t, webhook.Queue, 2)
})
t.Run("Webhook Check Back Online", func(t *testing.T) {
assert.True(t, webhook.Online)
})
t.Run("Webhook OnSuccess Again", func(t *testing.T) {
webhook.OnSuccess(TestService)
assert.Len(t, webhook.Queue, 2)
})
t.Run("Webhook Send", func(t *testing.T) {
err := webhook.Send(fullMsg)
assert.Nil(t, err)
assert.Len(t, webhook.Queue, 2)
})
t.Run("Webhook Queue", func(t *testing.T) {
go notifier.Queue(webhook)
time.Sleep(5 * time.Second)
assert.Equal(t, WEBHOOK_URL, webhook.Host)
assert.Equal(t, 1, len(webhook.Queue))
})
}

View File

@ -2,13 +2,19 @@
{{$n := .Select}}
<form method="POST" class="{{underscore $n.Method }}" action="/settings/notifier/{{ $n.Method }}">
{{if $n.Title}}<h4>{{$n.Title}}</h4>{{end}}
{{if $n.Description}}<p class="small text-muted">{{safe $n.Description}}</p>{{end}}
{{if $n.Description}}<p class="small text-muted">{{$n.Description}}</p>{{end}}
{{range .Form}}
{{range $n.Form}}
<div class="form-group">
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
<input type="{{.Type}}" name="{{underscore .DbField}}" class="form-control" value="{{ $n.GetValue .DbField }}" id="{{underscore .Title}}" placeholder="{{.Placeholder}}" {{if .Required}}required{{end}}>
{{if .SmallText}}<small class="form-text text-muted">{{safe .SmallText}}</small>{{end}}
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
{{if eq .Type "textarea"}}
<textarea rows="3" class="form-control" name="{{underscore .DbField}}" id="{{underscore .Title}}">{{ $n.GetValue .DbField }}</textarea>
{{else}}
<input type="{{.Type}}" name="{{underscore .DbField}}" class="form-control" value="{{ $n.GetValue .DbField }}" id="{{underscore .Title}}" placeholder="{{.Placeholder}}" {{if .Required}}required{{end}}>
{{end}}
{{if .SmallText}}
<small class="form-text text-muted">{{safe .SmallText}}</small>
{{end}}
</div>
{{end}}
@ -26,10 +32,10 @@
</div>
<div class="col-3 col-sm-2 mt-1">
<span class="switch">
<input type="checkbox" name="enable" class="switch" id="switch-{{ $n.Method }}" {{if $n.Enabled}}checked{{end}}>
<label for="switch-{{ $n.Method }}"></label>
</span>
<span class="switch">
<input type="checkbox" name="enable" class="switch" id="switch-{{ $n.Method }}" {{if $n.Enabled}}checked{{end}}>
<label for="switch-{{ $n.Method }}"></label>
</span>
</div>
<input type="hidden" name="notifier" value="{{underscore $n.Method }}">

View File

@ -1,5 +1,5 @@
{{define "form_service"}}
<form action="{{if ne .Id 0}}/service/{{.Id}}{{else}}/service{{end}}" method="POST">
<form action="{{if ne .Id 0}}/service/{{.Id}}{{else}}/services{{end}}" method="POST">
<div class="form-group row">
<label for="service_name" class="col-sm-4 col-form-label">Service Name</label>
<div class="col-sm-8">

View File

@ -1,5 +1,5 @@
{{define "form_user"}}
<form action="{{if ne .Id 0}}/user/{{.Id}}{{else}}/user{{end}}" method="POST">
<form action="{{if ne .Id 0}}/user/{{.Id}}{{else}}/users{{end}}" method="POST">
<div class="form-group row">
<label for="username" class="col-sm-4 col-form-label">Username</label>
<div class="col-6 col-md-4">