From ce36a834189e971897b212acb6940ca2517b5bf5 Mon Sep 17 00:00:00 2001 From: Medzik <8584366-Medzik@users.noreply.gitlab.com> Date: Mon, 12 Jul 2021 12:52:44 +0200 Subject: [PATCH] Simple Update - Update Backend - Change DB Connect from init to Connect() - Simple Update API --- .gitignore | 1 - backend/ping.go | 23 ++++++++++++++++++----- backend/run.go | 4 +++- config/pingbot.go | 5 +++++ config/version.go | 2 +- database/{init.go => connect.go} | 2 +- go.mod | 1 + go.sum | 9 ++------- init.go | 24 ++++++++++++++++++++++++ main.go | 20 ++------------------ website/routes/api/getAll.go | 4 ++-- website/routes/api/status.go | 1 + 12 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 config/pingbot.go rename database/{init.go => connect.go} (97%) create mode 100644 init.go diff --git a/.gitignore b/.gitignore index 677e091..67b5431 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -pingbot* # Sums MD5SUM diff --git a/backend/ping.go b/backend/ping.go index c36e24b..eb45b1d 100644 --- a/backend/ping.go +++ b/backend/ping.go @@ -3,18 +3,19 @@ package backend import ( "net/http" + "github.com/tcnksm/go-httpstat" "gitlab.com/gaming0skar123/go/pingbot/common" "gitlab.com/gaming0skar123/go/pingbot/database" ) -var AmountSuccess uint8 -var AmountErr uint8 +var AmountSuccess uint +var AmountErr uint var checkErr = common.CheckErr func ping() { results, err := database.GetAll() - if checkErr(err, "Get All res from DB") { + if checkErr(err, "get all from db") { return } @@ -24,8 +25,20 @@ func ping() { } func loop(value database.URL) { - r, err := http.Get(value.URL) - if checkErr(err, "Ping URL") { + req, err := http.NewRequest("GET", value.URL, nil) + if checkErr(err, "new http request") { + AmountErr++ + return + } + + var result httpstat.Result + + ctx := httpstat.WithHTTPStat(req.Context(), &result) + req = req.WithContext(ctx) + + client := http.DefaultClient + r, err := client.Do(req) + if checkErr(err, "ping url") { AmountErr++ return } diff --git a/backend/run.go b/backend/run.go index 2c2a9c7..e73255a 100644 --- a/backend/run.go +++ b/backend/run.go @@ -2,12 +2,14 @@ package backend import ( "time" + + "gitlab.com/gaming0skar123/go/pingbot/config" ) func Run() { ping() - ticker := time.NewTicker(2 * time.Minute) + ticker := time.NewTicker(config.PingBot_Ticker) quit := make(chan struct{}) diff --git a/config/pingbot.go b/config/pingbot.go new file mode 100644 index 0000000..6a53458 --- /dev/null +++ b/config/pingbot.go @@ -0,0 +1,5 @@ +package config + +import "time" + +var PingBot_Ticker = 2 * time.Minute diff --git a/config/version.go b/config/version.go index ed4865c..841898b 100644 --- a/config/version.go +++ b/config/version.go @@ -1,3 +1,3 @@ package config -var Version = "1.0.0-beta.1" +var Version = "1.0.0-beta.2" diff --git a/database/init.go b/database/connect.go similarity index 97% rename from database/init.go rename to database/connect.go index eb9eef1..9e4bb67 100644 --- a/database/init.go +++ b/database/connect.go @@ -13,7 +13,7 @@ import ( var Client *mongo.Client var DB *mongo.Database -func init() { +func Connect() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() diff --git a/go.mod b/go.mod index 333e7f8..cafd994 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/modern-go/reflect2 v1.0.1 // indirect github.com/posener/complete v1.2.3 // indirect github.com/sirupsen/logrus v1.8.1 + github.com/tcnksm/go-httpstat v0.2.0 github.com/ugorji/go v1.2.6 // indirect github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect go.mongodb.org/mongo-driver v1.5.4 diff --git a/go.sum b/go.sum index 4d8980d..f89662c 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= -github.com/aws/aws-sdk-go v1.39.3 h1:JMDk7p+AV89MdVy/ZcFWAGivWIE3vXOsRriFjFWVcIY= -github.com/aws/aws-sdk-go v1.39.3/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q= github.com/aws/aws-sdk-go v1.39.4 h1:nXBChUaG5cinrl3yg4/rUyssOOLH/ohk4S9K03kJirE= github.com/aws/aws-sdk-go v1.39.4/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= @@ -67,11 +65,9 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= @@ -126,7 +122,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/posener/complete v1.2.2-0.20190308074557-af07aa5181b3 h1:GqpA1/5oN1NgsxoSA4RH0YWTaqvUlQNeOpHXD/JRbOQ= github.com/posener/complete v1.2.2-0.20190308074557-af07aa5181b3/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E= github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo= github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= @@ -147,6 +142,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tcnksm/go-httpstat v0.2.0 h1:rP7T5e5U2HfmOBmZzGgGZjBQ5/GluWUylujl0tJ04I0= +github.com/tcnksm/go-httpstat v0.2.0/go.mod h1:s3JVJFtQxtBEBC9dwcdTTXS9xFnM3SXAZwPG41aurT8= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= @@ -171,8 +168,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= -golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= diff --git a/init.go b/init.go new file mode 100644 index 0000000..d7ab328 --- /dev/null +++ b/init.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/jpillora/opts" + "gitlab.com/gaming0skar123/go/pingbot/update" +) + +type CMDOptions struct { + Update bool `opts:"help=automatic updates"` +} + +func init() { + cmd := CMDOptions{ + Update: true, + } + + opts.Parse(&cmd) + + if cmd.Update { + go update.Updater() + } else { + log.Warn("Auto Update -> Disabled") + } +} diff --git a/main.go b/main.go index eab301c..3811236 100644 --- a/main.go +++ b/main.go @@ -1,35 +1,19 @@ package main import ( - "github.com/jpillora/opts" "gitlab.com/gaming0skar123/go/pingbot/backend" "gitlab.com/gaming0skar123/go/pingbot/common" "gitlab.com/gaming0skar123/go/pingbot/config" - _ "gitlab.com/gaming0skar123/go/pingbot/database" - "gitlab.com/gaming0skar123/go/pingbot/update" + "gitlab.com/gaming0skar123/go/pingbot/database" "gitlab.com/gaming0skar123/go/pingbot/website" ) -type CMDOptions struct { - Update bool `opts:"help=automatic updates"` -} - var log = common.Log func main() { - cmd := CMDOptions{ - Update: true, - } - - opts.Parse(&cmd) - log.Info("You're using verion: ", config.Version) - if cmd.Update { - go update.Updater() - } else { - log.Warn("Auto Update -> Disabled") - } + database.Connect() go website.Server() backend.Run() diff --git a/website/routes/api/getAll.go b/website/routes/api/getAll.go index f1bbdd2..022b184 100644 --- a/website/routes/api/getAll.go +++ b/website/routes/api/getAll.go @@ -40,7 +40,7 @@ func GetAll(c *gin.Context) { db = append(db, dbType{ URL: value.URL, - HASH: hash, + Hash: hash, }) } @@ -52,5 +52,5 @@ func GetAll(c *gin.Context) { type dbType struct { URL string - HASH string + Hash string } diff --git a/website/routes/api/status.go b/website/routes/api/status.go index feb5b43..2fdca96 100644 --- a/website/routes/api/status.go +++ b/website/routes/api/status.go @@ -11,6 +11,7 @@ func Status(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "success": true, "ping": gin.H{ + "all": backend.AmountSuccess + backend.AmountErr, "success": backend.AmountSuccess, "err": backend.AmountErr, },