- license: changed from MIT to 0BSD
- database: added retry to connect
- api: modified status
- config: moved to files env and no-env
- other modified: nodemon.json, .gitignore
This commit is contained in:
Medzik 2021-07-24 09:50:17 +00:00
parent 13771d1394
commit f1921ff8b1
12 changed files with 53 additions and 52 deletions

12
.gitignore vendored
View File

@ -8,16 +8,8 @@
*.dll *.dll
*.so *.so
*.dylib *.dylib
# Test binary, built with `go test -c`
*.test
# Output binary
*.out *.out
dist/ dist/
# Sums # Test binary, built with `go test -c`
MD5SUM *.test
SHA256SUM
VERSION

27
LICENSE
View File

@ -1,21 +1,14 @@
MIT License The BSD Zero Clause License (0BSD)
Copyright (c) 2021 Medzik Copyright (c) 2021 Medzik
Permission is hereby granted, free of charge, to any person obtaining a copy Permission to use, copy, modify, and/or distribute this software for any
of this software and associated documentation files (the "Software"), to deal purpose with or without fee is hereby granted.
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:
The above copyright notice and this permission notice shall be included in all THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
copies or substantial portions of the Software. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER PERFORMANCE OF THIS SOFTWARE.
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.

13
config/no-env.go Normal file
View File

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

View File

@ -1,5 +0,0 @@
package config
import "time"
var PingBot_Ticker = 2 * time.Minute

View File

@ -1,8 +0,0 @@
package config
import (
"time"
)
var GH_Repo = "MedzikUser/go-pingbot"
var Latest_Version_Check = 2 * time.Minute

View File

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

View File

@ -1,3 +0,0 @@
package config
var Port = ":8080"

View File

@ -15,18 +15,32 @@ import (
var Client mongo.Client var Client mongo.Client
var Coll *mongo.Collection 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) 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, "connect to db") { 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()) err = Client.Ping(ctx, readpref.Primary())
if common.CheckErr(err, "ping db") { 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) Coll = Client.Database(config.Mongo_DB).Collection(config.Mongo_Collection)

View File

@ -7,7 +7,8 @@ import (
"go.mongodb.org/mongo-driver/mongo" "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() defer cancel()
result, err := Coll.InsertOne(ctx, url) result, err := Coll.InsertOne(ctx, url)

View File

@ -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 || exit 1" "exec": "clear && go build -o pingbot.out || exit 1 && ./pingbot.out || echo \"[\\033[0;31mPROGRAM PANIC\\033[0m]\" || exit 1"
} }

View File

@ -21,14 +21,14 @@ func Status(c *gin.Context) {
pid := os.Getpid() pid := os.Getpid()
c.JSON(http.StatusOK, json{ c.JSON(http.StatusOK, json{
"success": true,
"ping": json{ "ping": json{
"all": backend.AmountSuccess + backend.AmountErr, "all": backend.AmountSuccess + backend.AmountErr,
"success": backend.AmountSuccess, "success": backend.AmountSuccess,
"err": backend.AmountErr, "err": backend.AmountErr,
}, },
"stats": json{ "sys": json{
"pid": pid, "pid": pid,
"os": runtime.GOOS,
"mem": json{ "mem": json{
"alloc": mb(m.Alloc), "alloc": mb(m.Alloc),
"totalalloc": mb(m.TotalAlloc), "totalalloc": mb(m.TotalAlloc),
@ -38,9 +38,13 @@ func Status(c *gin.Context) {
"cpu": json{ "cpu": json{
"usage": cpu(pid), "usage": cpu(pid),
"num": runtime.NumCPU(), "num": runtime.NumCPU(),
"arch": runtime.GOARCH,
}, },
}, },
"v": config.Version, "v": json{
"go": runtime.Version(),
"release": config.Version,
},
}) })
} }