2021-07-11 21:19:37 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2021-07-31 20:59:43 +00:00
|
|
|
"context"
|
2021-07-11 21:19:37 +00:00
|
|
|
"net/http"
|
2021-07-31 20:59:43 +00:00
|
|
|
"time"
|
2021-07-11 21:19:37 +00:00
|
|
|
|
|
|
|
"gitlab.com/gaming0skar123/go/pingbot/common"
|
2021-07-29 18:51:15 +00:00
|
|
|
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
|
2021-07-11 21:19:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var checkErr = common.CheckErr
|
|
|
|
|
|
|
|
func ping() {
|
2021-07-29 18:51:15 +00:00
|
|
|
results, err := mongo.GetAll()
|
2021-07-31 20:59:43 +00:00
|
|
|
if checkErr(err, "get keys from db") {
|
2021-07-11 21:19:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range results {
|
|
|
|
go loop(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 18:51:15 +00:00
|
|
|
func loop(value mongo.URL) {
|
2021-07-31 20:59:43 +00:00
|
|
|
// Timeout 1 minute
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", value.URL, nil)
|
2021-07-12 10:52:44 +00:00
|
|
|
if checkErr(err, "new http request") {
|
2021-07-29 18:51:15 +00:00
|
|
|
Status.Error++
|
2021-07-12 10:52:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := http.DefaultClient
|
|
|
|
r, err := client.Do(req)
|
2021-07-31 20:59:43 +00:00
|
|
|
if err != nil {
|
2021-07-29 18:51:15 +00:00
|
|
|
Status.Error++
|
2021-07-11 21:19:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.StatusCode >= 200 && r.StatusCode < 400 {
|
2021-07-29 18:51:15 +00:00
|
|
|
Status.Success++
|
2021-07-11 21:19:37 +00:00
|
|
|
} else {
|
2021-07-29 18:51:15 +00:00
|
|
|
Status.Error++
|
2021-07-11 21:19:37 +00:00
|
|
|
}
|
|
|
|
}
|