chore(website): update getAll

This commit is contained in:
Medzik 2021-09-05 11:20:19 +00:00
parent b3decb1561
commit 22e7e1aa11
7 changed files with 12 additions and 45 deletions

View File

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

View File

@ -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) {

View File

@ -11,7 +11,7 @@ import (
var log = common.Log
func StopAfterPing() {
cache()
Cache()
num := ping()
// timeout

View File

@ -8,7 +8,7 @@ import (
func Ticker() {
// On Start
cache()
Cache()
ping()
ticker := time.NewTicker(config.Toml.Backend.Ping * time.Minute)

View File

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

View File

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

View File

@ -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,
})
}