diff --git a/backend/cache.go b/backend/cache.go index 81c8e82..25658d8 100644 --- a/backend/cache.go +++ b/backend/cache.go @@ -7,10 +7,10 @@ import ( "gitlab.com/gaming0skar123/go/pingbot/database/mongo" ) -var cacheURL []string +var CacheURL []string -func cache() { - cacheURL = cacheURL[:0] +func Cache() { + CacheURL = CacheURL[:0] err := common.Retry(1, 1*time.Second, func() error { results, err := mongo.GetAll() @@ -19,7 +19,7 @@ func cache() { } for _, value := range results { - cacheURL = append(cacheURL, value.URL) + CacheURL = append(CacheURL, value.URL) } return nil diff --git a/backend/ping.go b/backend/ping.go index 2c52416..01a6ed9 100644 --- a/backend/ping.go +++ b/backend/ping.go @@ -6,23 +6,16 @@ import ( "time" "github.com/MedzikUser/go-utils/common" - "gitlab.com/gaming0skar123/go/pingbot/config" ) -var cacheRetry int - func ping() int { - if cacheRetry >= config.Toml.Backend.Cache { - cache() - cacheRetry = 0 - } - cacheRetry++ + Cache() - for _, url := range cacheURL { + for _, url := range CacheURL { go loop(url) } - return len(cacheURL) + return len(CacheURL) } func loop(url string) { diff --git a/backend/stopAfterPing.go b/backend/stopAfterPing.go index eff13e3..4d14378 100644 --- a/backend/stopAfterPing.go +++ b/backend/stopAfterPing.go @@ -11,7 +11,7 @@ import ( var log = common.Log func StopAfterPing() { - cache() + Cache() num := ping() // timeout diff --git a/backend/ticker.go b/backend/ticker.go index 89ce4c7..131ba00 100644 --- a/backend/ticker.go +++ b/backend/ticker.go @@ -8,7 +8,7 @@ import ( func Ticker() { // On Start - cache() + Cache() ping() ticker := time.NewTicker(config.Toml.Backend.Ping * time.Minute) diff --git a/config.schema.toml b/config.schema.toml index 37acaaf..e328970 100644 --- a/config.schema.toml +++ b/config.schema.toml @@ -5,7 +5,6 @@ port=8080 # Port to listen [backend] enabled=true # Enable Backend ping=3 # Ping every three minutes -cache=5 # Get urls from db every five url pings [autoupdate] enabled=true # Enable Auto Updater diff --git a/config/toml.go b/config/toml.go index 5fed04c..58d2f25 100644 --- a/config/toml.go +++ b/config/toml.go @@ -27,7 +27,6 @@ type httpConfig struct { type backendConfig struct { Enabled bool Ping time.Duration - Cache int } type autoUpdateConfig struct { @@ -67,10 +66,6 @@ func init() { } } - if Toml.Backend.Cache == 0 { - Toml.Backend.Cache = 5 - } - if Toml.MongoDB.Collection != "" { Mongo_Collection = Toml.MongoDB.Collection } diff --git a/website/routes/api/getAll.go b/website/routes/api/getAll.go index ae99388..cc6672e 100644 --- a/website/routes/api/getAll.go +++ b/website/routes/api/getAll.go @@ -1,37 +1,17 @@ package api import ( - "fmt" "net/http" "github.com/gin-gonic/gin" - "gitlab.com/gaming0skar123/go/pingbot/database/mongo" + "gitlab.com/gaming0skar123/go/pingbot/backend" ) func GetAll(c *gin.Context) { - results, err := mongo.GetAll() - if err != nil { - c.JSON(http.StatusInternalServerError, json{ - "success": false, - "message": "Error get URLs from Database!", - }) - fmt.Println(err) - - return - } - - // DB Is Empty - if results == nil { - c.JSON(http.StatusNotFound, json{ - "success": false, - "message": "Database is empty!", - }) - - return - } + backend.Cache() c.JSON(http.StatusOK, json{ "success": true, - "db": results, + "db": backend.CacheURL, }) }