From 13771d1394dbadd87fcca9a530b1c6fe9332df53 Mon Sep 17 00:00:00 2001 From: Medzik <8584366-Medzik@users.noreply.gitlab.com> Date: Thu, 22 Jul 2021 08:23:49 +0000 Subject: [PATCH] v1.2.0 - database: added collection to var - website: changed gin.G to json type - api: status added cpu usage and move memory to stats - go: upgrade deps - update nodemon cfg and README --- README.md | 2 +- config/version.go | 2 +- database/connect.go | 10 ++++----- database/delete.go | 5 +---- database/getAll.go | 5 +---- database/insert.go | 8 ++------ go.mod | 1 + go.sum | 2 ++ nodemon.json | 2 +- website/routes/api/delete.go | 10 ++++----- website/routes/api/getAll.go | 7 +++---- website/routes/api/insert.go | 10 ++++----- website/routes/api/status.go | 40 ++++++++++++++++++++++++++++-------- website/routes/api/type.go | 3 +++ website/routes/index.go | 2 +- 15 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 website/routes/api/type.go diff --git a/README.md b/README.md index 28c8730..91a0a2f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ * Linux amd64 * [Download](https://github.com/MedzikUser/go-pingbot/releases) latest version * Unpack file `tar xzf pingbot_{VERSION}_linux_amd64.tar.gz` - * Create an .env and config.yml file and complete according to .env.schema and config.schema.yml + * Create an .env file and complete according to .env.schema * Add permissions `chmod +rwx pingbot.out` * Run binary `./pingbot.out` diff --git a/config/version.go b/config/version.go index 94a0e68..4176848 100644 --- a/config/version.go +++ b/config/version.go @@ -1,3 +1,3 @@ package config -var Version = "1.1.0" +var Version = "1.2.0" diff --git a/database/connect.go b/database/connect.go index ac0be5a..8396a9b 100644 --- a/database/connect.go +++ b/database/connect.go @@ -12,22 +12,22 @@ import ( "go.mongodb.org/mongo-driver/mongo/readpref" ) -var Client *mongo.Client -var DB *mongo.Database +var Client mongo.Client +var Coll *mongo.Collection func Connect() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() Client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.Mongo_URI)) - if common.CheckErr(err, "failed connecting to db") { + if common.CheckErr(err, "connect to db") { os.Exit(1) } err = Client.Ping(ctx, readpref.Primary()) - if common.CheckErr(err, "failed pinging db") { + if common.CheckErr(err, "ping db") { os.Exit(1) } - DB = Client.Database(config.Mongo_DB) + Coll = Client.Database(config.Mongo_DB).Collection(config.Mongo_Collection) } diff --git a/database/delete.go b/database/delete.go index 6c70060..205f1ba 100644 --- a/database/delete.go +++ b/database/delete.go @@ -4,17 +4,14 @@ import ( "context" "time" - "gitlab.com/gaming0skar123/go/pingbot/config" "go.mongodb.org/mongo-driver/mongo" ) func Delete(url *URL) (*mongo.DeleteResult, error) { - collection := DB.Collection(config.Mongo_Collection) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result, err := collection.DeleteOne(ctx, url) + result, err := Coll.DeleteOne(ctx, url) return result, err } diff --git a/database/getAll.go b/database/getAll.go index f885f9a..f863e79 100644 --- a/database/getAll.go +++ b/database/getAll.go @@ -4,19 +4,16 @@ import ( "context" "time" - "gitlab.com/gaming0skar123/go/pingbot/config" "go.mongodb.org/mongo-driver/bson" ) func GetAll() ([]URL, error) { - collection := DB.Collection(config.Mongo_Collection) - var results []URL ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - cursor, err := collection.Find(ctx, bson.D{{}}) + cursor, err := Coll.Find(ctx, bson.D{{}}) if err != nil { return nil, err } diff --git a/database/insert.go b/database/insert.go index 1540346..bd29617 100644 --- a/database/insert.go +++ b/database/insert.go @@ -4,17 +4,13 @@ import ( "context" "time" - "gitlab.com/gaming0skar123/go/pingbot/config" "go.mongodb.org/mongo-driver/mongo" ) -func Insert(url *URL) (*mongo.InsertOneResult, error) { - collection := DB.Collection(config.Mongo_Collection) - - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) +func Insert(url *URL) (*mongo.InsertOneResult, error) {ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - result, err := collection.InsertOne(ctx, url) + result, err := Coll.InsertOne(ctx, url) return result, err } diff --git a/go.mod b/go.mod index 1d6229e..e71487c 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/posener/complete v1.2.3 // indirect github.com/rhysd/go-github-selfupdate v1.2.3 github.com/sirupsen/logrus v1.8.1 + github.com/struCoder/pidusage v0.2.0 github.com/ugorji/go v1.2.6 // indirect github.com/ulikunitz/xz v0.5.10 // indirect github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect diff --git a/go.sum b/go.sum index 40b67e0..28b5f75 100644 --- a/go.sum +++ b/go.sum @@ -243,6 +243,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/struCoder/pidusage v0.2.0 h1:NXIoGYOVeQtWu0XF1fimtFDLNYQph/buI7hFTHyZyJY= +github.com/struCoder/pidusage v0.2.0/go.mod h1:bewtP2KUA1TBUyza5+/PCpSQ6sc/H6jJbIKAzqW86BA= github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw= github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= diff --git a/nodemon.json b/nodemon.json index 872ede2..fcedbd7 100644 --- a/nodemon.json +++ b/nodemon.json @@ -3,5 +3,5 @@ "ext": "go json", "signal": "SIGTERM", "quiet": true, - "exec": "clear && go build -o pingbot.out || exit 1 && ./pingbot.out" + "exec": "clear && go build -o pingbot.out || exit 1 && ./pingbot.out || exit 1" } diff --git a/website/routes/api/delete.go b/website/routes/api/delete.go index 52b5fa6..cf99f5b 100644 --- a/website/routes/api/delete.go +++ b/website/routes/api/delete.go @@ -13,7 +13,7 @@ func Delete(c *gin.Context) { d, err := base64.StdEncoding.DecodeString(url) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusBadRequest, json{ "success": false, "message": "Error Parsing Base64!", }) @@ -28,7 +28,7 @@ func Delete(c *gin.Context) { }) if r.DeletedCount <= 0 { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusNotFound, json{ "success": false, "message": "Not found!", }) @@ -37,7 +37,7 @@ func Delete(c *gin.Context) { } if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusInternalServerError, json{ "success": false, "message": "Error Deleting from Database!", }) @@ -45,8 +45,8 @@ func Delete(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + c.JSON(http.StatusOK, json{ "success": true, - "message": "Deleted", + "message": "Deleted!", }) } diff --git a/website/routes/api/getAll.go b/website/routes/api/getAll.go index a7e1e41..3dc0926 100644 --- a/website/routes/api/getAll.go +++ b/website/routes/api/getAll.go @@ -10,10 +10,9 @@ import ( func GetAll(c *gin.Context) { results, err := database.GetAll() - // Error Handling if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusInternalServerError, json{ "success": false, "message": "Error fetching URLs!", }) @@ -24,7 +23,7 @@ func GetAll(c *gin.Context) { // DB Is Empty if results == nil { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusNotFound, json{ "success": false, "message": "Database is empty!", }) @@ -40,7 +39,7 @@ func GetAll(c *gin.Context) { }) } - c.JSON(http.StatusOK, gin.H{ + c.JSON(http.StatusOK, json{ "success": true, "db": db, }) diff --git a/website/routes/api/insert.go b/website/routes/api/insert.go index cded59b..c65d68f 100644 --- a/website/routes/api/insert.go +++ b/website/routes/api/insert.go @@ -11,7 +11,7 @@ func Insert(c *gin.Context) { var post database.URL err := c.BindJSON(&post) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{ + c.JSON(http.StatusBadRequest, json{ "success": false, "message": "Error Binding JSON!", }) @@ -20,7 +20,7 @@ func Insert(c *gin.Context) { } if len(post.URL) < 1 { - c.JSON(http.StatusBadRequest, gin.H{ + c.JSON(http.StatusBadRequest, json{ "success": false, "message": "URL was not Provided!", }) @@ -30,7 +30,7 @@ func Insert(c *gin.Context) { _, err = http.Get(post.URL) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusBadRequest, json{ "success": false, "message": "Error Pinging URL!", }) @@ -42,7 +42,7 @@ func Insert(c *gin.Context) { URL: post.URL, }) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ + c.JSON(http.StatusInternalServerError, json{ "success": false, "message": "Error Inserting to Database!", }) @@ -50,7 +50,7 @@ func Insert(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{ + c.JSON(http.StatusOK, json{ "success": true, "url": post.URL, }) diff --git a/website/routes/api/status.go b/website/routes/api/status.go index ccb1a65..122b26a 100644 --- a/website/routes/api/status.go +++ b/website/routes/api/status.go @@ -2,11 +2,15 @@ package api import ( "fmt" + "math" "net/http" + "os" "runtime" "github.com/gin-gonic/gin" + "github.com/struCoder/pidusage" "gitlab.com/gaming0skar123/go/pingbot/backend" + "gitlab.com/gaming0skar123/go/pingbot/common" "gitlab.com/gaming0skar123/go/pingbot/config" ) @@ -14,23 +18,43 @@ func Status(c *gin.Context) { var m runtime.MemStats runtime.ReadMemStats(&m) - c.JSON(http.StatusOK, gin.H{ + pid := os.Getpid() + + c.JSON(http.StatusOK, json{ "success": true, - "ping": gin.H{ + "ping": json{ "all": backend.AmountSuccess + backend.AmountErr, "success": backend.AmountSuccess, "err": backend.AmountErr, }, - "mem": gin.H{ - "alloc": MB(m.Alloc), - "totalalloc": MB(m.TotalAlloc), - "sys": MB(m.Sys), - "numgc": m.NumGC, + "stats": json{ + "pid": pid, + "mem": json{ + "alloc": mb(m.Alloc), + "totalalloc": mb(m.TotalAlloc), + "sys": mb(m.Sys), + "numgc": m.NumGC, + }, + "cpu": json{ + "usage": cpu(pid), + "num": runtime.NumCPU(), + }, }, "v": config.Version, }) } -func MB(b uint64) string { +func mb(b uint64) string { return fmt.Sprintf("%d MB", b/1000/1000) } + +func cpu(pid int) *string { + sysInfo, err := pidusage.GetStat(pid) + if common.CheckErr(err, "get cpu stat") { + return nil + } + + s := fmt.Sprint(math.Round(sysInfo.CPU*100)/100, "%") + + return &s +} diff --git a/website/routes/api/type.go b/website/routes/api/type.go new file mode 100644 index 0000000..5297969 --- /dev/null +++ b/website/routes/api/type.go @@ -0,0 +1,3 @@ +package api + +type json map[string]interface{} diff --git a/website/routes/index.go b/website/routes/index.go index f28566e..db3332b 100644 --- a/website/routes/index.go +++ b/website/routes/index.go @@ -7,7 +7,7 @@ import ( ) func Index(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ + c.JSON(http.StatusOK, map[string]interface{}{ "success": true, }) }