From 71b7254fcea8f90f0e62646408372eb6a4a8649d Mon Sep 17 00:00:00 2001 From: hunterlong Date: Mon, 9 Mar 2020 08:15:15 -0700 Subject: [PATCH] ye --- Makefile | 5 +- cmd/cli.go | 4 +- cmd/database.go | 3 +- cmd/main.go | 30 +- database/interface.go | 16 - database/sample.go | 171 --------- frontend/src/API.js | 12 - .../src/components/Dashboard/EditService.vue | 6 +- frontend/src/forms/CoreSettings.vue | 37 +- frontend/src/forms/GithubAuth.vue | 80 +++++ frontend/src/forms/Login.vue | 11 +- frontend/src/forms/Service.vue | 18 +- frontend/src/pages/Settings.vue | 10 - go.mod | 1 + go.sum | 4 + handlers/integrations.go | 53 --- handlers/notifications.go | 8 +- handlers/prometheus.go | 3 +- handlers/routes.go | 6 +- handlers/services.go | 1 + handlers/setup.go | 42 ++- integrators/csv_file.go | 132 ------- integrators/csv_file_test.go | 43 --- integrators/docker.go | 104 ------ integrators/docker_test.go | 42 --- integrators/integrations.go | 169 --------- integrators/testdata/bulk_import.csv | 11 - integrators/traefik.go | 127 ------- integrators/traefik_test.go | 27 -- notifiers/command.go | 4 +- notifiers/command_test.go | 102 ------ notifiers/discord.go | 2 + notifiers/discord_test.go | 99 ------ notifiers/email.go | 2 + notifiers/email_test.go | 136 ------- notifiers/line_notify.go | 4 +- notifiers/mobile.go | 2 + notifiers/mobile_test.go | 109 ------ notifiers/notifiers.go | 48 ++- notifiers/notifiers_test.go | 84 ----- notifiers/slack.go | 2 + notifiers/slack_test.go | 115 ------ notifiers/telegram.go | 2 + notifiers/telegram_test.go | 105 ------ notifiers/twilio.go | 2 + notifiers/twilio_test.go | 109 ------ notifiers/webhook.go | 4 +- notifiers/webhook_test.go | 101 ------ types/checkins/samples.go | 19 +- types/configs/configs_test.go | 59 ---- types/configs/connection.go | 14 +- types/configs/database.go | 13 +- types/configs/file.go | 7 +- types/configs/migration.go | 6 +- types/core/init.go | 17 - types/core/samples.go | 5 +- types/core/struct.go | 7 + types/failures/samples.go | 6 +- types/groups/groups_test.go | 29 -- types/groups/samples.go | 15 +- types/hits/samples.go | 10 +- types/incidents/samples.go | 27 +- types/integrations/database.go | 41 --- types/integrations/struct.go | 25 -- types/messages/samples.go | 12 +- types/notifications/database.go | 19 +- types/notifications/events.go | 26 +- types/notifications/example_test.go | 179 ---------- types/notifications/methods.go | 54 +-- types/notifications/struct.go | 60 +--- types/notifications/struct_test.go | 212 ----------- types/services/database.go | 2 - types/services/samples.go | 24 +- types/services/services_checkin_test.go | 107 ------ types/services/services_test.go | 332 ------------------ types/time.go | 4 +- types/time_test.go | 8 - types/users/sample.go | 12 +- types/users/struct.go | 2 +- types/users/users_test.go | 117 ------ 80 files changed, 461 insertions(+), 3217 deletions(-) delete mode 100644 database/sample.go create mode 100644 frontend/src/forms/GithubAuth.vue delete mode 100644 handlers/integrations.go delete mode 100644 integrators/csv_file.go delete mode 100644 integrators/csv_file_test.go delete mode 100644 integrators/docker.go delete mode 100644 integrators/docker_test.go delete mode 100644 integrators/integrations.go delete mode 100644 integrators/testdata/bulk_import.csv delete mode 100644 integrators/traefik.go delete mode 100644 integrators/traefik_test.go delete mode 100644 notifiers/command_test.go delete mode 100644 notifiers/discord_test.go delete mode 100644 notifiers/email_test.go delete mode 100644 notifiers/mobile_test.go delete mode 100644 notifiers/notifiers_test.go delete mode 100644 notifiers/slack_test.go delete mode 100644 notifiers/telegram_test.go delete mode 100644 notifiers/twilio_test.go delete mode 100644 notifiers/webhook_test.go delete mode 100644 types/configs/configs_test.go delete mode 100644 types/groups/groups_test.go delete mode 100644 types/integrations/database.go delete mode 100644 types/integrations/struct.go delete mode 100644 types/notifications/example_test.go delete mode 100644 types/notifications/struct_test.go delete mode 100644 types/services/services_checkin_test.go delete mode 100644 types/services/services_test.go delete mode 100644 types/users/users_test.go diff --git a/Makefile b/Makefile index 5d625dc5..2f7c7024 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,9 @@ up: down: docker-compose -f docker-compose.yml -f dev/docker-compose.full.yml down --volumes --remove-orphans +test: clean + go test -v -p=1 -ldflags="-X main.VERSION=dev" -coverprofile=coverage.out ./... + lite: clean docker build -t hunterlong/statping:dev -f dev/Dockerfile.dev . docker-compose -f dev/docker-compose.lite.yml down @@ -107,7 +110,7 @@ clean: rm -rf ./{logs,assets,plugins,*.db,config.yml,.sass-cache,config.yml,statping,build,.sass-cache,index.html,vendor} rm -rf cmd/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log,*.html,*.json} rm -rf core/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log} - rm -rf core/notifier/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log} + rm -rf types/notifications/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log} rm -rf handlers/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log} rm -rf notifiers/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log} rm -rf source/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log} diff --git a/cmd/cli.go b/cmd/cli.go index 62259a5c..afd8cc04 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -106,7 +106,7 @@ func catchCLI(args []string) error { if err != nil { return err } - if err = configs.ConnectConfigs(config, false); err != nil { + if err = configs.ConnectConfigs(config); err != nil { return err } if data, err = handlers.ExportSettings(); err != nil { @@ -206,7 +206,7 @@ func runOnce() error { if err != nil { return errors.Wrap(err, "config.yml file not found") } - err = configs.ConnectConfigs(config, false) + err = configs.ConnectConfigs(config) if err != nil { return errors.Wrap(err, "issue connecting to database") } diff --git a/cmd/database.go b/cmd/database.go index 1037af39..23419eed 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -6,7 +6,6 @@ import ( "github.com/hunterlong/statping/types/groups" "github.com/hunterlong/statping/types/hits" "github.com/hunterlong/statping/types/incidents" - "github.com/hunterlong/statping/types/integrations" "github.com/hunterlong/statping/types/messages" "github.com/hunterlong/statping/types/notifications" "github.com/hunterlong/statping/types/services" @@ -19,5 +18,5 @@ var ( ) func init() { - DbModels = []interface{}{&services.Service{}, &users.User{}, &hits.Hit{}, &failures.Failure{}, &messages.Message{}, &groups.Group{}, &checkins.Checkin{}, &checkins.CheckinHit{}, ¬ifications.Notification{}, &incidents.Incident{}, &incidents.IncidentUpdate{}, &integrations.Integration{}} + DbModels = []interface{}{&services.Service{}, &users.User{}, &hits.Hit{}, &failures.Failure{}, &messages.Message{}, &groups.Group{}, &checkins.Checkin{}, &checkins.CheckinHit{}, ¬ifications.Notification{}, &incidents.Incident{}, &incidents.IncidentUpdate{}} } diff --git a/cmd/main.go b/cmd/main.go index 761376b2..c9c6f879 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,7 @@ import ( "flag" "fmt" "github.com/getsentry/sentry-go" + "github.com/hunterlong/statping/notifiers" "os" "os/signal" "syscall" @@ -125,14 +126,41 @@ func main() { } } - if err = configs.ConnectConfigs(c, true); err != nil { + if err = configs.ConnectConfigs(c); err != nil { exit(err) } + exists := database.DB().HasTable("core") + if !exists { + + if err := c.DropDatabase(); err != nil { + exit(errors.Wrap(err, "error dropping database")) + } + + if err := configs.CreateDatabase(); err != nil { + exit(errors.Wrap(err, "error creating database")) + } + + if err := configs.CreateAdminUser(c); err != nil { + exit(errors.Wrap(err, "error creating default admin user")) + } + + if err := configs.TriggerSamples(); err != nil { + exit(errors.Wrap(err, "error creating database")) + } + + } + if err := c.MigrateDatabase(); err != nil { exit(err) } + log.Infoln("Migrating Notifiers...") + if err := notifiers.Migrate(); err != nil { + exit(errors.Wrap(err, "error migrating notifiers")) + } + log.Infoln("Notifiers Migrated") + if err := mainProcess(); err != nil { exit(err) } diff --git a/database/interface.go b/database/interface.go index 60c4e9c8..594ba536 100644 --- a/database/interface.go +++ b/database/interface.go @@ -9,19 +9,3 @@ type DbObject interface { type Sampler interface { Sample() DbObject } - -func MigrateTable(table interface{}) error { - tx := database.Begin() - defer func() { - if r := recover(); r != nil { - tx.Rollback() - } - }() - - tx = tx.AutoMigrate(table) - - if err := tx.Commit().Error(); err != nil { - return err - } - return nil -} diff --git a/database/sample.go b/database/sample.go deleted file mode 100644 index b6e56559..00000000 --- a/database/sample.go +++ /dev/null @@ -1,171 +0,0 @@ -// Statping -// Copyright (C) 2018. Hunter Long and the project contributors -// Written by Hunter Long and the project contributors -// -// https://github.com/hunterlong/statping -// -// 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 . - -package database - -import ( - "time" - - _ "github.com/jinzhu/gorm/dialects/mysql" - _ "github.com/jinzhu/gorm/dialects/postgres" - _ "github.com/jinzhu/gorm/dialects/sqlite" -) - -var ( - sampleStart = time.Now().Add((-24 * 7) * time.Hour).UTC() - SampleHits = 9900. -) - -// InsertSampleHits will create a couple new hits for the sample services -//func InsertSampleHits() error { -// tx := Begin(&hits.Hit{}) -// sg := new(sync.WaitGroup) -// for i := int64(1); i <= 5; i++ { -// sg.Add(1) -// service := SelectService(i) -// seed := time.Now().UnixNano() -// log.Infoln(fmt.Sprintf("Adding %v sample hit records to service %v", SampleHits, service.Name)) -// createdAt := sampleStart -// p := utils.NewPerlin(2., 2., 10, seed) -// go func(sg *sync.WaitGroup) { -// defer sg.Done() -// for hi := 0.; hi <= float64(SampleHits); hi++ { -// latency := p.Noise1D(hi / 500) -// createdAt = createdAt.Add(60 * time.Second) -// hit := &hits.Hit{ -// Service: service.Id, -// CreatedAt: createdAt, -// Latency: latency, -// } -// tx = tx.Create(&hit) -// } -// }(sg) -// } -// sg.Wait() -// if err := tx.Commit().Error(); err != nil { -// log.Errorln(err) -// return types.ErrWrap(err, types.ErrorCreateSampleHits) -// } -// return nil -//} - -//func TmpRecords(dbFile string) error { -// var sqlFile = utils.Directory + "/" + dbFile -// if err := utils.CreateDirectory(utils.Directory + "/tmp"); err != nil { -// log.Error(err) -// } -// var tmpSqlFile = utils.Directory + "/tmp/" + dbFile -// SampleHits = 480 -// -// var err error -// CoreApp = NewCore() -// CoreApp.Name = "Tester" -// CoreApp.Setup = true -// configs := &types.DbConfig{ -// DbConn: "sqlite", -// Project: "Tester", -// Location: utils.Directory, -// SqlFile: sqlFile, -// } -// log.Infoln("saving config.yml in: " + utils.Directory) -// if err := configs.Save(utils.Directory); err != nil { -// log.Error(err) -// } -// -// log.Infoln("loading config.yml from: " + utils.Directory) -// if configs, err = LoadConfigs(); err != nil { -// log.Error(err) -// } -// log.Infoln("connecting to database") -// -// exists := utils.FileExists(tmpSqlFile) -// if exists { -// log.Infoln(tmpSqlFile + " was found, copying the temp database to " + sqlFile) -// if err := utils.DeleteFile(sqlFile); err != nil { -// log.Error(err) -// } -// if err := utils.CopyFile(tmpSqlFile, sqlFile); err != nil { -// log.Error(err) -// } -// log.Infoln("loading config.yml from: " + utils.Directory) -// -// if err := CoreApp.Connect(configs, false, utils.Directory); err != nil { -// log.Error(err) -// } -// log.Infoln("selecting the Core variable") -// if _, err := SelectCore(); err != nil { -// log.Error(err) -// } -// log.Infoln("inserting notifiers into database") -// if err := InsertNotifierDB(); err != nil { -// log.Error(err) -// } -// log.Infoln("inserting integrations into database") -// if err := InsertIntegratorDB(); err != nil { -// log.Error(err) -// } -// log.Infoln("loading all services") -// if _, err := SelectAllServices(false); err != nil { -// return err -// } -// if err := AttachNotifiers(); err != nil { -// log.Error(err) -// } -// if err := AddIntegrations(); err != nil { -// log.Error(err) -// } -// CoreApp.Notifications = notifier.AllCommunications -// return nil -// } -// -// log.Infoln(tmpSqlFile + " not found, creating a new database...") -// -// if err := CoreApp.Connect(configs, false, utils.Directory); err != nil { -// return err -// } -// log.Infoln("creating database") -// if err := CoreApp.CreateDatabase(); err != nil { -// return err -// } -// log.Infoln("migrating database") -// if err := MigrateDatabase(); err != nil { -// return err -// } -// log.Infoln("insert large sample data into database") -// if err := InsertLargeSampleData(); err != nil { -// return err -// } -// log.Infoln("selecting the Core variable") -// if CoreApp, err = SelectCore(); err != nil { -// return err -// } -// log.Infoln("inserting notifiers into database") -// if err := InsertNotifierDB(); err != nil { -// return err -// } -// log.Infoln("inserting integrations into database") -// if err := InsertIntegratorDB(); err != nil { -// return err -// } -// log.Infoln("loading all services") -// if _, err := SelectAllServices(false); err != nil { -// return err -// } -// log.Infoln("copying sql database file to: " + tmpSqlFile) -// if err := utils.CopyFile(sqlFile, tmpSqlFile); err != nil { -// return err -// } -// return err -//} diff --git a/frontend/src/API.js b/frontend/src/API.js index 0b857f05..e624c8c2 100644 --- a/frontend/src/API.js +++ b/frontend/src/API.js @@ -129,18 +129,6 @@ class Api { return axios.post('/api/notifier/' + data.method + '/test', data).then(response => (response.data)) } - async integrations() { - return axios.get('/api/integrations').then(response => (response.data)) - } - - async integration(name) { - return axios.get('/api/integrations/' + name).then(response => (response.data)) - } - - async integration_save(data) { - return axios.post('/api/integrations/' + data.name, data).then(response => (response.data)) - } - async renewApiKeys() { return axios.get('/api/renew').then(response => (response.data)) } diff --git a/frontend/src/components/Dashboard/EditService.vue b/frontend/src/components/Dashboard/EditService.vue index e215fee0..0c8e0a82 100644 --- a/frontend/src/components/Dashboard/EditService.vue +++ b/frontend/src/components/Dashboard/EditService.vue @@ -1,10 +1,6 @@ diff --git a/frontend/src/forms/CoreSettings.vue b/frontend/src/forms/CoreSettings.vue index eef738b0..c5b89fa8 100644 --- a/frontend/src/forms/CoreSettings.vue +++ b/frontend/src/forms/CoreSettings.vue @@ -87,6 +87,37 @@ +
+
+

Github Authentication

+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ + + + +
+
+ + + +
+
+ @@ -99,16 +130,14 @@ return { core: this.$store.getters.core, } - }, - async mounted() { - }, methods: { async saveSettings() { const c = this.core const coreForm = { name: c.name, description: c.description, domain: c.domain, - timezone: c.timezone, using_cdn: c.using_cdn, footer: c.footer, update_notify: c.update_notify + timezone: c.timezone, using_cdn: c.using_cdn, footer: c.footer, update_notify: c.update_notify, + gh_client_id: c.github_clientId, gh_client_secret: c.github_clientSecret } await Api.core_save(coreForm) const core = await Api.core() diff --git a/frontend/src/forms/GithubAuth.vue b/frontend/src/forms/GithubAuth.vue new file mode 100644 index 00000000..e1c51065 --- /dev/null +++ b/frontend/src/forms/GithubAuth.vue @@ -0,0 +1,80 @@ + + + + + + diff --git a/frontend/src/forms/Login.vue b/frontend/src/forms/Login.vue index 02750b31..d8ebb8a8 100644 --- a/frontend/src/forms/Login.vue +++ b/frontend/src/forms/Login.vue @@ -22,6 +22,7 @@ + @@ -37,9 +38,13 @@ auth: {}, loading: false, error: false, - disabled: true + disabled: true, + ghLoginURL: "" } }, + mounted() { + this.GHlogin() + }, methods: { checkForm() { if (!this.username || !this.password) { @@ -60,6 +65,10 @@ this.$router.push('/dashboard') } this.loading = false + }, + async GHlogin() { + const core = this.$store.getters.core; + this.ghLoginURL = `https://github.com/login/oauth/authorize?client_id=${core.gh_client_id}&redirect_uri=${core.domain}/oauth/callback&scope=user,repo` } } } diff --git a/frontend/src/forms/Service.vue b/frontend/src/forms/Service.vue index fae1acf3..1196136e 100644 --- a/frontend/src/forms/Service.vue +++ b/frontend/src/forms/Service.vue @@ -1,6 +1,8 @@