mirror of https://github.com/MedzikUser/go-pingbot
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
This commit is contained in:
parent
9c41f504ad
commit
13771d1394
|
@ -5,7 +5,7 @@
|
||||||
* Linux amd64
|
* Linux amd64
|
||||||
* [Download](https://github.com/MedzikUser/go-pingbot/releases) latest version
|
* [Download](https://github.com/MedzikUser/go-pingbot/releases) latest version
|
||||||
* Unpack file `tar xzf pingbot_{VERSION}_linux_amd64.tar.gz`
|
* 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`
|
* Add permissions `chmod +rwx pingbot.out`
|
||||||
* Run binary `./pingbot.out`
|
* Run binary `./pingbot.out`
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
var Version = "1.1.0"
|
var Version = "1.2.0"
|
||||||
|
|
|
@ -12,22 +12,22 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Client *mongo.Client
|
var Client mongo.Client
|
||||||
var DB *mongo.Database
|
var Coll *mongo.Collection
|
||||||
|
|
||||||
func Connect() {
|
func Connect() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
Client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.Mongo_URI))
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Client.Ping(ctx, readpref.Primary())
|
err = Client.Ping(ctx, readpref.Primary())
|
||||||
if common.CheckErr(err, "failed pinging db") {
|
if common.CheckErr(err, "ping db") {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
DB = Client.Database(config.Mongo_DB)
|
Coll = Client.Database(config.Mongo_DB).Collection(config.Mongo_Collection)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,14 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitlab.com/gaming0skar123/go/pingbot/config"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Delete(url *URL) (*mongo.DeleteResult, error) {
|
func Delete(url *URL) (*mongo.DeleteResult, error) {
|
||||||
collection := DB.Collection(config.Mongo_Collection)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
result, err := collection.DeleteOne(ctx, url)
|
result, err := Coll.DeleteOne(ctx, url)
|
||||||
|
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,19 +4,16 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitlab.com/gaming0skar123/go/pingbot/config"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetAll() ([]URL, error) {
|
func GetAll() ([]URL, error) {
|
||||||
collection := DB.Collection(config.Mongo_Collection)
|
|
||||||
|
|
||||||
var results []URL
|
var results []URL
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
cursor, err := collection.Find(ctx, bson.D{{}})
|
cursor, err := Coll.Find(ctx, bson.D{{}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,13 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitlab.com/gaming0skar123/go/pingbot/config"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Insert(url *URL) (*mongo.InsertOneResult, error) {
|
func Insert(url *URL) (*mongo.InsertOneResult, error) {ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
collection := DB.Collection(config.Mongo_Collection)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
result, err := collection.InsertOne(ctx, url)
|
result, err := Coll.InsertOne(ctx, url)
|
||||||
|
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -23,6 +23,7 @@ require (
|
||||||
github.com/posener/complete v1.2.3 // indirect
|
github.com/posener/complete v1.2.3 // indirect
|
||||||
github.com/rhysd/go-github-selfupdate v1.2.3
|
github.com/rhysd/go-github-selfupdate v1.2.3
|
||||||
github.com/sirupsen/logrus v1.8.1
|
github.com/sirupsen/logrus v1.8.1
|
||||||
|
github.com/struCoder/pidusage v0.2.0
|
||||||
github.com/ugorji/go v1.2.6 // indirect
|
github.com/ugorji/go v1.2.6 // indirect
|
||||||
github.com/ulikunitz/xz v0.5.10 // indirect
|
github.com/ulikunitz/xz v0.5.10 // indirect
|
||||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||||
|
|
2
go.sum
2
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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
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/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 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
|
||||||
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
|
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
|
||||||
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
||||||
|
|
|
@ -3,5 +3,5 @@
|
||||||
"ext": "go json",
|
"ext": "go json",
|
||||||
"signal": "SIGTERM",
|
"signal": "SIGTERM",
|
||||||
"quiet": true,
|
"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"
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ func Delete(c *gin.Context) {
|
||||||
|
|
||||||
d, err := base64.StdEncoding.DecodeString(url)
|
d, err := base64.StdEncoding.DecodeString(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusBadRequest, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Error Parsing Base64!",
|
"message": "Error Parsing Base64!",
|
||||||
})
|
})
|
||||||
|
@ -28,7 +28,7 @@ func Delete(c *gin.Context) {
|
||||||
})
|
})
|
||||||
|
|
||||||
if r.DeletedCount <= 0 {
|
if r.DeletedCount <= 0 {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusNotFound, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Not found!",
|
"message": "Not found!",
|
||||||
})
|
})
|
||||||
|
@ -37,7 +37,7 @@ func Delete(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Error Deleting from Database!",
|
"message": "Error Deleting from Database!",
|
||||||
})
|
})
|
||||||
|
@ -45,8 +45,8 @@ func Delete(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, json{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "Deleted",
|
"message": "Deleted!",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,9 @@ import (
|
||||||
|
|
||||||
func GetAll(c *gin.Context) {
|
func GetAll(c *gin.Context) {
|
||||||
results, err := database.GetAll()
|
results, err := database.GetAll()
|
||||||
|
|
||||||
// Error Handling
|
// Error Handling
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Error fetching URLs!",
|
"message": "Error fetching URLs!",
|
||||||
})
|
})
|
||||||
|
@ -24,7 +23,7 @@ func GetAll(c *gin.Context) {
|
||||||
|
|
||||||
// DB Is Empty
|
// DB Is Empty
|
||||||
if results == nil {
|
if results == nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusNotFound, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Database is empty!",
|
"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,
|
"success": true,
|
||||||
"db": db,
|
"db": db,
|
||||||
})
|
})
|
||||||
|
|
|
@ -11,7 +11,7 @@ func Insert(c *gin.Context) {
|
||||||
var post database.URL
|
var post database.URL
|
||||||
err := c.BindJSON(&post)
|
err := c.BindJSON(&post)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Error Binding JSON!",
|
"message": "Error Binding JSON!",
|
||||||
})
|
})
|
||||||
|
@ -20,7 +20,7 @@ func Insert(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(post.URL) < 1 {
|
if len(post.URL) < 1 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "URL was not Provided!",
|
"message": "URL was not Provided!",
|
||||||
})
|
})
|
||||||
|
@ -30,7 +30,7 @@ func Insert(c *gin.Context) {
|
||||||
|
|
||||||
_, err = http.Get(post.URL)
|
_, err = http.Get(post.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusBadRequest, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Error Pinging URL!",
|
"message": "Error Pinging URL!",
|
||||||
})
|
})
|
||||||
|
@ -42,7 +42,7 @@ func Insert(c *gin.Context) {
|
||||||
URL: post.URL,
|
URL: post.URL,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, json{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Error Inserting to Database!",
|
"message": "Error Inserting to Database!",
|
||||||
})
|
})
|
||||||
|
@ -50,7 +50,7 @@ func Insert(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, json{
|
||||||
"success": true,
|
"success": true,
|
||||||
"url": post.URL,
|
"url": post.URL,
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,11 +2,15 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/struCoder/pidusage"
|
||||||
"gitlab.com/gaming0skar123/go/pingbot/backend"
|
"gitlab.com/gaming0skar123/go/pingbot/backend"
|
||||||
|
"gitlab.com/gaming0skar123/go/pingbot/common"
|
||||||
"gitlab.com/gaming0skar123/go/pingbot/config"
|
"gitlab.com/gaming0skar123/go/pingbot/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,23 +18,43 @@ func Status(c *gin.Context) {
|
||||||
var m runtime.MemStats
|
var m runtime.MemStats
|
||||||
runtime.ReadMemStats(&m)
|
runtime.ReadMemStats(&m)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
pid := os.Getpid()
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, json{
|
||||||
"success": true,
|
"success": true,
|
||||||
"ping": gin.H{
|
"ping": json{
|
||||||
"all": backend.AmountSuccess + backend.AmountErr,
|
"all": backend.AmountSuccess + backend.AmountErr,
|
||||||
"success": backend.AmountSuccess,
|
"success": backend.AmountSuccess,
|
||||||
"err": backend.AmountErr,
|
"err": backend.AmountErr,
|
||||||
},
|
},
|
||||||
"mem": gin.H{
|
"stats": json{
|
||||||
"alloc": MB(m.Alloc),
|
"pid": pid,
|
||||||
"totalalloc": MB(m.TotalAlloc),
|
"mem": json{
|
||||||
"sys": MB(m.Sys),
|
"alloc": mb(m.Alloc),
|
||||||
"numgc": m.NumGC,
|
"totalalloc": mb(m.TotalAlloc),
|
||||||
|
"sys": mb(m.Sys),
|
||||||
|
"numgc": m.NumGC,
|
||||||
|
},
|
||||||
|
"cpu": json{
|
||||||
|
"usage": cpu(pid),
|
||||||
|
"num": runtime.NumCPU(),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"v": config.Version,
|
"v": config.Version,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func MB(b uint64) string {
|
func mb(b uint64) string {
|
||||||
return fmt.Sprintf("%d MB", b/1000/1000)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
type json map[string]interface{}
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Index(c *gin.Context) {
|
func Index(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, map[string]interface{}{
|
||||||
"success": true,
|
"success": true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue