Skip to content

Commit

Permalink
notifier updates - discord - organize
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Sep 12, 2018
1 parent ce1dc32 commit 685197b
Show file tree
Hide file tree
Showing 31 changed files with 706 additions and 609 deletions.
5 changes: 3 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ source/rice-box.go
.DS_Store
*/**/node_modules
vendor

!build/alpine-linux-amd64
servers
dev
!build/alpine-linux-amd64
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.57
VERSION=0.58
BINARY_NAME=statup
GOPATH:=$(GOPATH)
GOCMD=go
Expand Down Expand Up @@ -159,7 +159,7 @@ docker-push-cypress:
docker push hunterlong/statup:cypress

# push the :latest tag to Docker hub
docker-push-latest: docker
docker-push-latest:
docker push hunterlong/statup:latest

# push the :base and :base-v{VERSION} tag to Docker hub
Expand Down
7 changes: 3 additions & 4 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/handlers"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/joho/godotenv"
"io/ioutil"
Expand Down Expand Up @@ -149,10 +148,10 @@ func RunOnce() {
if err != nil {
utils.Log(4, err)
}
for _, s := range core.CoreApp.Services {
out := s.(*types.Service)
for _, out := range core.CoreApp.Services {
service := out.Select()
out.Check(true)
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", out.Name, out.Domain, (out.Latency * 1000), out.Online)
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", service.Name, service.Domain, (service.Latency * 1000), service.Online)
}
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/core/notifier"
"github.com/hunterlong/statup/handlers"
"github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestRunAll(t *testing.T) {
})
t.Run(dbt+" Select Comms", func(t *testing.T) {
t.SkipNow()
RunSelectAllMysqlCommunications(t)
RunSelectAllNotifiers(t)
})
t.Run(dbt+" Create Users", func(t *testing.T) {
RunUser_Create(t)
Expand Down Expand Up @@ -290,15 +290,16 @@ func RunSelectCoreMYQL(t *testing.T, db string) {

func RunSelectAllMysqlServices(t *testing.T) {
var err error
t.SkipNow()
services, err := core.CoreApp.SelectAllServices()
assert.Nil(t, err)
assert.Equal(t, 18, len(services))
}

func RunSelectAllMysqlCommunications(t *testing.T) {
func RunSelectAllNotifiers(t *testing.T) {
var err error
notifiers.Collections = core.DbSession.Table("communication").Model(&notifiers.Notification{})
comms := notifiers.Load()
notifier.SetDB(core.DbSession)
comms := notifier.Load()
assert.Nil(t, err)
assert.Equal(t, 3, len(comms))
}
Expand Down Expand Up @@ -473,7 +474,7 @@ func RunDeleteService(t *testing.T) {
func RunCreateService_Hits(t *testing.T) {
services := core.CoreApp.Services
assert.NotNil(t, services)
assert.Equal(t, 37, len(services))
assert.Equal(t, 19, len(services))
for _, service := range services {
service.Check(true)
assert.NotNil(t, service)
Expand Down Expand Up @@ -528,7 +529,7 @@ func RunPrometheusHandler(t *testing.T) {
rr := httptest.NewRecorder()
route.ServeHTTP(rr, req)
t.Log(rr.Body.String())
assert.True(t, strings.Contains(rr.Body.String(), "statup_total_services 37"))
assert.True(t, strings.Contains(rr.Body.String(), "statup_total_services 19"))
assert.True(t, handlers.IsAuthenticated(req))
}

Expand Down
6 changes: 3 additions & 3 deletions core/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package core
import (
"bytes"
"fmt"
"github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/core/notifier"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"io/ioutil"
Expand Down Expand Up @@ -224,7 +224,7 @@ func RecordSuccess(s *Service) {
}
utils.Log(1, fmt.Sprintf("Service %v Successful: %0.2f ms", s.Name, hit.Latency*1000))
s.CreateHit(hit)
notifiers.OnSuccess(s.Service)
notifier.OnSuccess(s.Service)
}

// RecordFailure will create a new 'failure' record in the database for a offline service
Expand All @@ -237,5 +237,5 @@ func RecordFailure(s *Service, issue string) {
}
utils.Log(2, fmt.Sprintf("Service %v Failing: %v", s.Name, issue))
s.CreateFailure(fail)
notifiers.OnFailure(s.Service, fail)
notifier.OnFailure(s.Service, fail)
}
2 changes: 1 addition & 1 deletion core/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ReturnCheckin(s *types.Checkin) *Checkin {

func FindCheckin(api string) *types.Checkin {
for _, ser := range CoreApp.Services {
service := ser.(*types.Service)
service := ser.Select()
for _, c := range service.Checkins {
if c.Api == api {
return c
Expand Down
21 changes: 7 additions & 14 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package core

import (
"github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/core/notifier"
_ "github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
Expand Down Expand Up @@ -57,21 +58,21 @@ func (c *Core) ToCore() *types.Core {
// InitApp will initialize Statup
func InitApp() {
SelectCore()
InsertNotifierDB()
insertNotifierDB()
CoreApp.SelectAllServices()
CheckServices()
CoreApp.Communications = notifiers.Load()
CoreApp.Notifications = notifier.Load()
go DatabaseMaintence()
}

func InsertNotifierDB() error {
func insertNotifierDB() error {
if DbSession == nil {
err := Configs.Connect(false, utils.Directory)
if err != nil {
return errors.New("database connection has not been created")
}
}
notifiers.Collections = commDB()
notifier.SetDB(DbSession)
return nil
}

Expand All @@ -81,14 +82,6 @@ func UpdateCore(c *Core) (*Core, error) {
return c, db.Error
}

func (c *Core) Notifiers() []notifiers.Notification {
var n []notifiers.Notification
for _, c := range c.Communications {
n = append(n, c.(notifiers.Notification))
}
return n
}

// UsingAssets will return true if /assets folder is present
func (c Core) UsingAssets() bool {
return source.UsingAssets(utils.Directory)
Expand Down Expand Up @@ -122,7 +115,7 @@ func (c Core) MobileSASS() string {
// AllOnline will be true if all services are online
func (c Core) AllOnline() bool {
for _, s := range CoreApp.Services {
if !s.(*types.Service).Online {
if !s.Select().Online {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestSelectCore(t *testing.T) {
}

func TestInsertNotifierDB(t *testing.T) {
err := InsertNotifierDB()
err := insertNotifierDB()
assert.Nil(t, err)
}

Expand Down
10 changes: 5 additions & 5 deletions core/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package core
import (
"fmt"
"github.com/go-yaml/yaml"
"github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/core/notifier"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -66,7 +66,7 @@ func usersDB() *gorm.DB {

// commDB returns the 'communications' database column
func commDB() *gorm.DB {
return DbSession.Table("communication").Model(&notifiers.Notification{})
return DbSession.Model(&notifier.Notification{})
}

// hitsDB returns the 'hits' database column
Expand Down Expand Up @@ -252,7 +252,7 @@ func (db *DbConfig) SeedDatabase() (string, string, error) {
func (db *DbConfig) DropDatabase() error {
utils.Log(1, "Dropping Database Tables...")
err := DbSession.DropTableIfExists("checkins")
err = DbSession.DropTableIfExists("communication")
err = DbSession.DropTableIfExists("notifications")
err = DbSession.DropTableIfExists("core")
err = DbSession.DropTableIfExists("failures")
err = DbSession.DropTableIfExists("hits")
Expand All @@ -265,7 +265,7 @@ func (db *DbConfig) DropDatabase() error {
func (db *DbConfig) CreateDatabase() error {
utils.Log(1, "Creating Database Tables...")
err := DbSession.CreateTable(&types.Checkin{})
err = DbSession.Table("communication").CreateTable(&notifiers.Notification{})
err = DbSession.CreateTable(&notifier.Notification{})
err = DbSession.Table("core").CreateTable(&types.Core{})
err = DbSession.CreateTable(&types.Failure{})
err = DbSession.CreateTable(&types.Hit{})
Expand All @@ -290,7 +290,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{}).Table("core").AutoMigrate(&types.Core{}).Table("communication").AutoMigrate(&notifiers.Notification{})
tx = tx.AutoMigrate(&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Checkin{}, &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))
Expand Down
82 changes: 0 additions & 82 deletions core/events.go

This file was deleted.

3 changes: 1 addition & 2 deletions core/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"fmt"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"html/template"
"io/ioutil"
Expand All @@ -39,7 +38,7 @@ func ExportIndexHTML() string {
CoreApp.SelectAllServices()
CoreApp.UseCdn = true
for _, srv := range CoreApp.Services {
service := srv.(*types.Service)
service := srv.(*Service)
service.Check(true)
fmt.Println(service.Name, service.Online, service.Latency)
}
Expand Down
46 changes: 46 additions & 0 deletions core/notifier/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <[email protected]> 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 notifier

import (
"errors"
"fmt"
"strings"
)

var (
allowed_vars = []string{"host", "username", "password", "port", "api_key", "api_secret", "var1", "var2"}
)

func checkNotifierForm(n Notifier) error {
notifier := n.Select()
for _, f := range notifier.Form {
contains := contains(f.DbField, allowed_vars)
if !contains {
return errors.New(fmt.Sprintf("the DbField '%v' is not allowed, allowed vars: %v", f.DbField, allowed_vars))
}
}
return nil
}

func contains(s string, arr []string) bool {
for _, v := range arr {
if strings.ToLower(s) == v {
return true
}
}
return false
}
Loading

0 comments on commit 685197b

Please sign in to comment.