diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 252cd43..8b6578b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.goreleaser.yml b/.goreleaser.yml index e5f3027..b7bce03 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -23,6 +23,7 @@ archives: - LICENSE - .env.schema - config.schema.toml + - README.md checksum: name_template: 'md5_checksums.txt' diff --git a/Makefile b/Makefile index 3b9a5d3..716c921 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ BINARY_NAME=pingbot.out build: go build -o ${BINARY_NAME} +snapshot: + goreleaser --snapshot --rm-dist + run: go run . diff --git a/backend/ping.go b/backend/ping.go index 4f591ea..d47c537 100644 --- a/backend/ping.go +++ b/backend/ping.go @@ -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 } diff --git a/common/log.go b/common/log.go index 35cdf8c..12b4e92 100644 --- a/common/log.go +++ b/common/log.go @@ -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, diff --git a/config/constant.go b/config/constant.go new file mode 100644 index 0000000..e349c78 --- /dev/null +++ b/config/constant.go @@ -0,0 +1,4 @@ +package config + +const GH_Repo = "MedzikUser/go-pingbot" +const Version = "dev" diff --git a/config/no-env.go b/config/no-env.go deleted file mode 100644 index 4594461..0000000 --- a/config/no-env.go +++ /dev/null @@ -1,3 +0,0 @@ -package config - -var GH_Repo = "MedzikUser/go-pingbot" diff --git a/config/version.go b/config/version.go deleted file mode 100644 index f356490..0000000 --- a/config/version.go +++ /dev/null @@ -1,3 +0,0 @@ -package config - -var Version = "dev" diff --git a/database/mongo/getAll.go b/database/mongo/getAll.go index a50be5e..1c99be1 100644 --- a/database/mongo/getAll.go +++ b/database/mongo/getAll.go @@ -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 } diff --git a/database/mongo/type.go b/database/mongo/type.go index b41b389..7e6bac9 100644 --- a/database/mongo/type.go +++ b/database/mongo/type.go @@ -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{} diff --git a/main.go b/main.go index 9eee5ff..a3b0650 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,4 @@ func main() { } wg.Wait() - - //c := make(chan struct{}) - //<-c } diff --git a/nodemon.sh b/nodemon.sh old mode 100644 new mode 100755 diff --git a/renovate.json b/renovate.json index f45d8f1..bfb4665 100644 --- a/renovate.json +++ b/renovate.json @@ -1,5 +1,11 @@ { "extends": [ "config:base" + ], + "packageRules": [ + { + "matchUpdateTypes": ["minor", "patch"], + "automerge": true + } ] } diff --git a/website/routes/api/getAll.go b/website/routes/api/getAll.go index 6a0e8fb..b5af974 100644 --- a/website/routes/api/getAll.go +++ b/website/routes/api/getAll.go @@ -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, }) } diff --git a/website/routes/api/insert.go b/website/routes/api/insert.go index 9c1edde..6548219 100644 --- a/website/routes/api/insert.go +++ b/website/routes/api/insert.go @@ -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{ diff --git a/website/routes/api/status.go b/website/routes/api/status.go index df33074..0ba2adf 100644 --- a/website/routes/api/status.go +++ b/website/routes/api/status.go @@ -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, + }, }) } diff --git a/website/routes/index.go b/website/routes/index.go index db3332b..f28566e 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, map[string]interface{}{ + c.JSON(http.StatusOK, gin.H{ "success": true, }) } diff --git a/website/server.go b/website/server.go index f9c5118..ace8804 100644 --- a/website/server.go +++ b/website/server.go @@ -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) }