- 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
This commit is contained in:
Medzik 2021-07-22 08:23:49 +00:00
parent 9c41f504ad
commit 13771d1394
15 changed files with 64 additions and 45 deletions

View File

@ -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`

View File

@ -1,3 +1,3 @@
package config
var Version = "1.1.0"
var Version = "1.2.0"

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"
}

View File

@ -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!",
})
}

View File

@ -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,
})

View File

@ -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,
})

View File

@ -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
}

View File

@ -0,0 +1,3 @@
package api
type json map[string]interface{}

View File

@ -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,
})
}