diff --git a/.gitignore b/.gitignore index 566adb5..94ab595 100644 --- a/.gitignore +++ b/.gitignore @@ -8,16 +8,8 @@ *.dll *.so *.dylib - -# Test binary, built with `go test -c` -*.test - -# Output binary *.out dist/ -# Sums -MD5SUM -SHA256SUM - -VERSION +# Test binary, built with `go test -c` +*.test diff --git a/LICENSE b/LICENSE index efe39b3..d022b6a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,14 @@ -MIT License +The BSD Zero Clause License (0BSD) Copyright (c) 2021 Medzik -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/config/mongodb.go b/config/env.go similarity index 100% rename from config/mongodb.go rename to config/env.go diff --git a/config/no-env.go b/config/no-env.go new file mode 100644 index 0000000..33cc0cb --- /dev/null +++ b/config/no-env.go @@ -0,0 +1,13 @@ +package config + +import "time" + +// backend +var PingBot_Ticker = 2 * time.Minute + +// update +var GH_Repo = "MedzikUser/go-pingbot" +var Latest_Version_Check = 2 * time.Minute + +// website +var Port = ":8080" diff --git a/config/pingbot.go b/config/pingbot.go deleted file mode 100644 index 6a53458..0000000 --- a/config/pingbot.go +++ /dev/null @@ -1,5 +0,0 @@ -package config - -import "time" - -var PingBot_Ticker = 2 * time.Minute diff --git a/config/update.go b/config/update.go deleted file mode 100644 index c4cda8d..0000000 --- a/config/update.go +++ /dev/null @@ -1,8 +0,0 @@ -package config - -import ( - "time" -) - -var GH_Repo = "MedzikUser/go-pingbot" -var Latest_Version_Check = 2 * time.Minute diff --git a/config/version.go b/config/version.go index 4176848..2a0ef3d 100644 --- a/config/version.go +++ b/config/version.go @@ -1,3 +1,3 @@ package config -var Version = "1.2.0" +var Version = "1.3.0" diff --git a/config/website.go b/config/website.go deleted file mode 100644 index 5aeec8b..0000000 --- a/config/website.go +++ /dev/null @@ -1,3 +0,0 @@ -package config - -var Port = ":8080" diff --git a/database/connect.go b/database/connect.go index 8396a9b..d1b175d 100644 --- a/database/connect.go +++ b/database/connect.go @@ -15,18 +15,32 @@ import ( var Client mongo.Client var Coll *mongo.Collection -func Connect() { +func Connect(retry ...int8) { + if len(retry) == 0 { + retry = append(retry, 0) + } + + if retry[0] == 2 { + os.Exit(1) + } + 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, "connect to db") { - os.Exit(1) + time.Sleep(2 * time.Second) + Connect(retry[0] + 1) + + return } err = Client.Ping(ctx, readpref.Primary()) if common.CheckErr(err, "ping db") { - os.Exit(1) + time.Sleep(2 * time.Second) + Connect(retry[0] + 1) + + return } Coll = Client.Database(config.Mongo_DB).Collection(config.Mongo_Collection) diff --git a/database/insert.go b/database/insert.go index bd29617..765234f 100644 --- a/database/insert.go +++ b/database/insert.go @@ -7,7 +7,8 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -func Insert(url *URL) (*mongo.InsertOneResult, error) {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 := Coll.InsertOne(ctx, url) diff --git a/nodemon.json b/nodemon.json index fcedbd7..04e49b7 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 || exit 1" + "exec": "clear && go build -o pingbot.out || exit 1 && ./pingbot.out || echo \"[\\033[0;31mPROGRAM PANIC\\033[0m]\" || exit 1" } diff --git a/website/routes/api/status.go b/website/routes/api/status.go index 122b26a..45be571 100644 --- a/website/routes/api/status.go +++ b/website/routes/api/status.go @@ -21,14 +21,14 @@ func Status(c *gin.Context) { pid := os.Getpid() c.JSON(http.StatusOK, json{ - "success": true, "ping": json{ "all": backend.AmountSuccess + backend.AmountErr, "success": backend.AmountSuccess, "err": backend.AmountErr, }, - "stats": json{ + "sys": json{ "pid": pid, + "os": runtime.GOOS, "mem": json{ "alloc": mb(m.Alloc), "totalalloc": mb(m.TotalAlloc), @@ -38,9 +38,13 @@ func Status(c *gin.Context) { "cpu": json{ "usage": cpu(pid), "num": runtime.NumCPU(), + "arch": runtime.GOARCH, }, }, - "v": config.Version, + "v": json{ + "go": runtime.Version(), + "release": config.Version, + }, }) }