Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
chore(website): update getAll
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Sep 5, 2021
1 parent b3decb1 commit 22e7e1a
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 45 deletions.
8 changes: 4 additions & 4 deletions backend/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -19,7 +19,7 @@ func cache() {
}

for _, value := range results {
cacheURL = append(cacheURL, value.URL)
CacheURL = append(CacheURL, value.URL)
}

return nil
Expand Down
13 changes: 3 additions & 10 deletions backend/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion backend/stopAfterPing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var log = common.Log

func StopAfterPing() {
cache()
Cache()
num := ping()

// timeout
Expand Down
2 changes: 1 addition & 1 deletion backend/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func Ticker() {
// On Start
cache()
Cache()
ping()

ticker := time.NewTicker(config.Toml.Backend.Ping * time.Minute)
Expand Down
1 change: 0 additions & 1 deletion config.schema.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type httpConfig struct {
type backendConfig struct {
Enabled bool
Ping time.Duration
Cache int
}

type autoUpdateConfig struct {
Expand Down Expand Up @@ -67,10 +66,6 @@ func init() {
}
}

if Toml.Backend.Cache == 0 {
Toml.Backend.Cache = 5
}

if Toml.MongoDB.Collection != "" {
Mongo_Collection = Toml.MongoDB.Collection
}
Expand Down
26 changes: 3 additions & 23 deletions website/routes/api/getAll.go
Original file line number Diff line number Diff line change
@@ -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,
})
}

0 comments on commit 22e7e1a

Please sign in to comment.