From 05ff5c5e2ec4e689d95fb014f1ae12978fde0cb0 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Wed, 18 Dec 2019 03:36:29 -0800 Subject: [PATCH 1/2] temp --- Makefile | 12 ++++++++++- core/notifier/notifiers.go | 16 ++++++++++++++ handlers/routes.go | 1 + handlers/services.go | 4 ++++ notifiers/webhook.go | 16 +------------- source/tmpl/settings.gohtml | 43 +++++++++++++++++++++++-------------- 6 files changed, 60 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 8f560f62..5a7a26b1 100644 --- a/Makefile +++ b/Makefile @@ -71,10 +71,20 @@ install: build run: build ./$(BINARY_NAME) --ip 0.0.0.0 --port 8080 +# run Statping with Delve for debugging +rundlv: + dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./$(BINARY_NAME) + +builddlv: + $(GOBUILD) -gcflags "all=-N -l" -o ./$(BINARY_NAME) -v ./cmd + +watch: + reflex -v -r '\.go' -s -- sh -c 'make builddlv && make rundlv' + # compile assets using SASS and Rice. compiles scss -> css, and run rice embed-go compile: generate sass source/scss/base.scss source/css/base.css - cd source && $(GOPATH)/bin/rice embed-go + cd source && rice embed-go rm -rf .sass-cache # benchmark testing diff --git a/core/notifier/notifiers.go b/core/notifier/notifiers.go index 1de0e1b6..0e3d86ac 100644 --- a/core/notifier/notifiers.go +++ b/core/notifier/notifiers.go @@ -461,3 +461,19 @@ func (n *Notification) IsRunning() bool { return true } } + +// ExampleService can be used for the OnTest() method for notifiers +var ExampleService = &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: types.NewNullString("test example"), + LastResponse: "this is an example response", + CreatedAt: time.Now().Add(-24 * time.Hour), +} diff --git a/handlers/routes.go b/handlers/routes.go index bd1ca48e..2c3bce0d 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -92,6 +92,7 @@ func Router() *mux.Router { r.Handle("/service/{id}", http.HandlerFunc(servicesViewHandler)).Methods("GET") r.Handle("/service/{id}/edit", authenticated(servicesViewHandler, true)).Methods("GET") r.Handle("/service/{id}/delete_failures", authenticated(servicesDeleteFailuresHandler, true)).Methods("GET") + r.Handle("/service/create", authenticated(createServiceHandler, true)).Methods("GET") r.Handle("/group/{id}", http.HandlerFunc(groupViewHandler)).Methods("GET") diff --git a/handlers/services.go b/handlers/services.go index 8873926b..453b9050 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -335,3 +335,7 @@ func apiServiceHitsHandler(w http.ResponseWriter, r *http.Request) { returnJson(hits, w, r) } + +func createServiceHandler(w http.ResponseWriter, r *http.Request) { + ExecuteResponse(w, r, "service_create.gohtml", core.CoreApp, nil) +} diff --git a/notifiers/webhook.go b/notifiers/webhook.go index c76b69a9..41b1e03a 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -129,21 +129,7 @@ func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) { } func (w *webhooker) 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: types.NewNullString("test example"), - LastResponse: "this is an example response", - CreatedAt: time.Now().Add(-24 * time.Hour), - } - body := replaceBodyText(w.Var2, service, nil) + body := replaceBodyText(w.Var2, notifier.ExampleService, nil) resp, err := w.sendHttpWebhook(body) if err != nil { return err diff --git a/source/tmpl/settings.gohtml b/source/tmpl/settings.gohtml index 11954288..664789a3 100644 --- a/source/tmpl/settings.gohtml +++ b/source/tmpl/settings.gohtml @@ -7,10 +7,12 @@
-
-
- - - - - - - Enabling this will send only notifications when the status of a services changes. - - - -
-
-
@@ -172,6 +159,30 @@
+
+

Notifications

+ +
+ +
+
+ + + + + + + Enabling this will send only notifications when the status of a services changes. + + + +
+
+ + +
+
+
{{if not .UsingAssets }} From d9e03871f3e859e61ab1787a653b2bc37b6c0942 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Thu, 19 Dec 2019 08:48:59 -0800 Subject: [PATCH 2/2] signal graceful method, create service url --- Makefile | 14 ++++++++++--- cmd/main.go | 12 +++++++++++ core/sample.go | 4 ++++ handlers/routes.go | 2 +- source/scss/mobile.scss | 2 +- source/tmpl/form_service.gohtml | 31 ++++++++++++++++----------- source/tmpl/messages.gohtml | 6 +++--- source/tmpl/service_create.gohtml | 9 ++++++++ source/tmpl/services.gohtml | 16 +++++++------- source/tmpl/settings.gohtml | 35 ++++++++++++++----------------- source/tmpl/users.gohtml | 4 ++-- source/wiki.go | 4 ++-- 12 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 source/tmpl/service_create.gohtml diff --git a/Makefile b/Makefile index 5a7a26b1..5dc8564c 100644 --- a/Makefile +++ b/Makefile @@ -73,13 +73,21 @@ run: build # run Statping with Delve for debugging rundlv: - dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./$(BINARY_NAME) + lsof -ti:8080 | xargs kill + DB_CONN=sqlite DB_HOST=localhost DB_DATABASE=sqlite DB_PASS=none DB_USER=none GO_ENV=test \ + dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./statping + +killdlv: + lsof -ti:2345 | xargs kill builddlv: $(GOBUILD) -gcflags "all=-N -l" -o ./$(BINARY_NAME) -v ./cmd watch: - reflex -v -r '\.go' -s -- sh -c 'make builddlv && make rundlv' + find . -print | grep -i '.*\.\(go\|gohtml\)' | justrun -v -c \ + 'go build -v -gcflags "all=-N -l" -o statping ./cmd && make rundlv &' \ + -delay 10s -stdin \ + -i="Makefile,statping,statup.db,statup.db-journal,handlers/graphql/generated.go" # compile assets using SASS and Rice. compiles scss -> css, and run rice embed-go compile: generate @@ -250,7 +258,7 @@ clean: rm -rf dev/test/cypress/videos rm -f coverage.* sass rm -f source/rice-box.go - rm -f *.db-journal + rm -rf **/*.db-journal rm -rf *.snap find . -name "*.out" -type f -delete find . -name "*.cpu" -type f -delete diff --git a/cmd/main.go b/cmd/main.go index b0990e07..1463ed4f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -25,6 +25,8 @@ import ( "github.com/hunterlong/statping/utils" "github.com/joho/godotenv" "os" + "os/signal" + "syscall" ) var ( @@ -61,6 +63,7 @@ func parseFlags() { // main will run the Statping application func main() { var err error + go sigterm() parseFlags() loadDotEnvs() source.Assets() @@ -92,6 +95,15 @@ func main() { mainProcess() } +// sigterm will attempt to close the database connections gracefully +func sigterm() { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL) + <-sigs + core.CloseDB() + os.Exit(1) +} + // loadDotEnvs attempts to load database configs from a '.env' file in root directory func loadDotEnvs() error { err := godotenv.Load() diff --git a/core/sample.go b/core/sample.go index 20d3c68f..f53fb386 100644 --- a/core/sample.go +++ b/core/sample.go @@ -44,6 +44,7 @@ func InsertSampleData() error { Order: 1, GroupId: 1, Permalink: types.NewNullString("google"), + VerifySSL: types.NewNullBool(true), CreatedAt: createdOn, }) s2 := ReturnService(&types.Service{ @@ -56,6 +57,7 @@ func InsertSampleData() error { Timeout: 20, Order: 2, Permalink: types.NewNullString("statping_github"), + VerifySSL: types.NewNullBool(true), CreatedAt: createdOn, }) s3 := ReturnService(&types.Service{ @@ -68,6 +70,7 @@ func InsertSampleData() error { Timeout: 30, Order: 3, Public: types.NewNullBool(true), + VerifySSL: types.NewNullBool(true), GroupId: 2, CreatedAt: createdOn, }) @@ -83,6 +86,7 @@ func InsertSampleData() error { Timeout: 30, Order: 4, Public: types.NewNullBool(true), + VerifySSL: types.NewNullBool(true), GroupId: 2, CreatedAt: createdOn, }) diff --git a/handlers/routes.go b/handlers/routes.go index 2c3bce0d..44af5554 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -89,10 +89,10 @@ func Router() *mux.Router { // SERVICE Routes r.Handle("/services", authenticated(servicesHandler, true)).Methods("GET") + r.Handle("/service/create", authenticated(createServiceHandler, true)).Methods("GET") r.Handle("/service/{id}", http.HandlerFunc(servicesViewHandler)).Methods("GET") r.Handle("/service/{id}/edit", authenticated(servicesViewHandler, true)).Methods("GET") r.Handle("/service/{id}/delete_failures", authenticated(servicesDeleteFailuresHandler, true)).Methods("GET") - r.Handle("/service/create", authenticated(createServiceHandler, true)).Methods("GET") r.Handle("/group/{id}", http.HandlerFunc(groupViewHandler)).Methods("GET") diff --git a/source/scss/mobile.scss b/source/scss/mobile.scss index 0ae37874..f5d4cdca 100644 --- a/source/scss/mobile.scss +++ b/source/scss/mobile.scss @@ -48,7 +48,7 @@ .card-body { font-size: 10pt; - padding: 0px 10px; + padding: 10px 10px; } .lg_number { diff --git a/source/tmpl/form_service.gohtml b/source/tmpl/form_service.gohtml index f9ee9376..964b84e2 100644 --- a/source/tmpl/form_service.gohtml +++ b/source/tmpl/form_service.gohtml @@ -7,6 +7,7 @@ {{else}}
{{end}} +

Basic Information

@@ -33,6 +34,21 @@ Statping will attempt to connect to this URL
+
+ +
+ + Attach this service to a group +
+
+ +

Request Details

+
@@ -80,6 +96,9 @@
+ +

Additional Options

+
@@ -138,18 +157,6 @@
-
- -
- - Attach this service to a group -
-
diff --git a/source/tmpl/messages.gohtml b/source/tmpl/messages.gohtml index 5ec025e4..4d19672e 100644 --- a/source/tmpl/messages.gohtml +++ b/source/tmpl/messages.gohtml @@ -4,7 +4,7 @@ {{template "nav"}} {{if .}}
-

Messages

+

Messages

@@ -34,7 +34,7 @@ {{end}} {{if Auth}}
-

Create Message

+

Create Message

{{template "form_message" NewMessage}}
{{end}} @@ -56,4 +56,4 @@ $(document).ready(function() { }); }); -{{end}} \ No newline at end of file +{{end}} diff --git a/source/tmpl/service_create.gohtml b/source/tmpl/service_create.gohtml new file mode 100644 index 00000000..86bb0712 --- /dev/null +++ b/source/tmpl/service_create.gohtml @@ -0,0 +1,9 @@ +{{define "title"}}Statping | Create Service{{end}} +{{define "content"}} +
+{{template "nav"}} +
+ {{template "form_service" NewService}} +
+
+{{end}} diff --git a/source/tmpl/services.gohtml b/source/tmpl/services.gohtml index f97252d4..99ce6b60 100644 --- a/source/tmpl/services.gohtml +++ b/source/tmpl/services.gohtml @@ -5,7 +5,9 @@
{{if ne (len .Services) 0}} -

Services

+

Services + Create +

@@ -21,7 +23,7 @@ @@ -36,14 +38,10 @@
{{.Name}} {{if .Online}}ONLINE{{else}}OFFLINE{{end}} - + {{if .Public.Bool}}PUBLIC{{else}}PRIVATE{{end}} {{if ne .GroupId 0}}{{(Group .GroupId).Name}}{{end}}
{{end}} - {{if Auth}} -

Create Service

- {{template "form_service" NewService}} - {{end}}
-
-

Groups

+
+

Groups

@@ -70,7 +68,7 @@
{{if Auth}} -

Create Group

+

Create Group

{{template "form_group" NewGroup}} {{end}}
diff --git a/source/tmpl/settings.gohtml b/source/tmpl/settings.gohtml index 664789a3..465f9d96 100644 --- a/source/tmpl/settings.gohtml +++ b/source/tmpl/settings.gohtml @@ -6,13 +6,16 @@