mirror of
https://github.com/MedzikUser/go-pingbot
synced 2024-08-14 23:57:07 +00:00
feat: add ctx to backend ping, add cluster functional...
- Added ctx to backend ping - Log changed Stderr to Stdout - Added cluster functional - Updated more files
This commit is contained in:
parent
ea7fb2774f
commit
689e833dbb
18 changed files with 59 additions and 30 deletions
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
|
@ -13,6 +13,8 @@ jobs:
|
|||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: '0'
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v2
|
||||
|
|
|
@ -23,6 +23,7 @@ archives:
|
|||
- LICENSE
|
||||
- .env.schema
|
||||
- config.schema.toml
|
||||
- README.md
|
||||
|
||||
checksum:
|
||||
name_template: 'md5_checksums.txt'
|
||||
|
|
3
Makefile
3
Makefile
|
@ -3,6 +3,9 @@ BINARY_NAME=pingbot.out
|
|||
build:
|
||||
go build -o ${BINARY_NAME}
|
||||
|
||||
snapshot:
|
||||
goreleaser --snapshot --rm-dist
|
||||
|
||||
run:
|
||||
go run .
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitlab.com/gaming0skar123/go/pingbot/common"
|
||||
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
|
||||
|
@ -11,7 +13,7 @@ var checkErr = common.CheckErr
|
|||
|
||||
func ping() {
|
||||
results, err := mongo.GetAll()
|
||||
if checkErr(err, "get all from db") {
|
||||
if checkErr(err, "get keys from db") {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -21,7 +23,11 @@ func ping() {
|
|||
}
|
||||
|
||||
func loop(value mongo.URL) {
|
||||
req, err := http.NewRequest("GET", value.URL, nil)
|
||||
// Timeout 1 minute
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", value.URL, nil)
|
||||
if checkErr(err, "new http request") {
|
||||
Status.Error++
|
||||
return
|
||||
|
@ -29,7 +35,7 @@ func loop(value mongo.URL) {
|
|||
|
||||
client := http.DefaultClient
|
||||
r, err := client.Do(req)
|
||||
if checkErr(err, "ping url") {
|
||||
if err != nil {
|
||||
Status.Error++
|
||||
return
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
var Log = &logrus.Logger{
|
||||
Out: os.Stderr,
|
||||
Out: os.Stdout,
|
||||
Formatter: new(logrus.TextFormatter),
|
||||
Hooks: make(logrus.LevelHooks),
|
||||
Level: logrus.InfoLevel,
|
||||
|
|
4
config/constant.go
Normal file
4
config/constant.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
package config
|
||||
|
||||
const GH_Repo = "MedzikUser/go-pingbot"
|
||||
const Version = "dev"
|
|
@ -1,3 +0,0 @@
|
|||
package config
|
||||
|
||||
var GH_Repo = "MedzikUser/go-pingbot"
|
|
@ -1,3 +0,0 @@
|
|||
package config
|
||||
|
||||
var Version = "dev"
|
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"gitlab.com/gaming0skar123/go/pingbot/config"
|
||||
)
|
||||
|
||||
func GetAll() ([]URL, error) {
|
||||
|
@ -13,7 +13,9 @@ func GetAll() ([]URL, error) {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := Coll.Find(ctx, bson.D{{}})
|
||||
cursor, err := Coll.Find(ctx, json{
|
||||
"cluster": config.Toml.Cluster.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package mongo
|
||||
|
||||
type URL struct {
|
||||
URL string `bson:"_id"`
|
||||
URL string `bson:"_id"`
|
||||
Cluster int `bson:"cluster"`
|
||||
}
|
||||
|
||||
type json map[string]interface{}
|
||||
|
|
3
main.go
3
main.go
|
@ -42,7 +42,4 @@ func main() {
|
|||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
//c := make(chan struct{})
|
||||
//<-c
|
||||
}
|
||||
|
|
0
nodemon.sh
Normal file → Executable file
0
nodemon.sh
Normal file → Executable file
|
@ -1,5 +1,11 @@
|
|||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
],
|
||||
"packageRules": [
|
||||
{
|
||||
"matchUpdateTypes": ["minor", "patch"],
|
||||
"automerge": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -31,14 +31,8 @@ func GetAll(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var db []string
|
||||
|
||||
for _, value := range results {
|
||||
db = append(db, value.URL)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, json{
|
||||
"success": true,
|
||||
"db": db,
|
||||
"db": results,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -38,8 +38,13 @@ func Insert(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if post.Cluster == 0 {
|
||||
post.Cluster = 1
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(&mongo.URL{
|
||||
URL: post.URL,
|
||||
URL: post.URL,
|
||||
Cluster: post.Cluster,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, json{
|
||||
|
|
|
@ -20,12 +20,20 @@ func Status(c *gin.Context) {
|
|||
|
||||
pid := os.Getpid()
|
||||
|
||||
c.JSON(http.StatusOK, json{
|
||||
"ping": json{
|
||||
var ping json
|
||||
|
||||
if config.Toml.Backend.Enabled {
|
||||
ping = json{
|
||||
"all": backend.Status.Error + backend.Status.Success,
|
||||
"success": backend.Status.Success,
|
||||
"err": backend.Status.Error,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
ping = nil
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, json{
|
||||
"ping": ping,
|
||||
"sys": json{
|
||||
"pid": pid,
|
||||
"os": runtime.GOOS,
|
||||
|
@ -45,6 +53,10 @@ func Status(c *gin.Context) {
|
|||
"go": runtime.Version(),
|
||||
"release": config.Version,
|
||||
},
|
||||
"node": json{
|
||||
"cluster": config.Toml.Cluster.ID,
|
||||
"node": config.Toml.Cluster.Node,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func Index(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, map[string]interface{}{
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package website
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -27,7 +27,7 @@ func Server() {
|
|||
|
||||
api.ApplyRoutes(router)
|
||||
|
||||
err := router.Run(fmt.Sprint(":", config.Toml.HTTP.Port))
|
||||
err := router.Run(":" + strconv.Itoa(config.Toml.HTTP.Port))
|
||||
if common.CheckErr(err, "gin start") {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue