feat: Add Cache URLs and Database config from toml file

- Added Cache URLs from Database
- and Database Name and Collection set from toml config file
This commit is contained in:
Medzik 2021-08-11 10:35:51 +00:00
parent f09209d1bf
commit 3432a74d23
7 changed files with 66 additions and 14 deletions

View File

@ -1,4 +1,2 @@
# MongoDB
MONGODB_URI=mongodb+srv://...
MONGODB_DB=...
MONGODB_COLLECTION=...

20
backend/cache.go Normal file
View File

@ -0,0 +1,20 @@
package backend
import (
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
)
var cacheURL []string
func cache() {
cacheURL = cacheURL[:0]
results, err := mongo.GetAll()
if checkErr(err, "get keys from db") {
return
}
for _, value := range results {
cacheURL = append(cacheURL, value.URL)
}
}

View File

@ -3,31 +3,43 @@ package backend
import (
"context"
"net/http"
"sync"
"time"
"gitlab.com/gaming0skar123/go/pingbot/common"
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
"gitlab.com/gaming0skar123/go/pingbot/config"
)
var checkErr = common.CheckErr
var (
checkErr = common.CheckErr
cacheRetry int
)
func ping() {
results, err := mongo.GetAll()
if checkErr(err, "get keys from db") {
return
if cacheRetry >= config.Toml.Backend.Cache {
cache()
cacheRetry = 0
}
cacheRetry++
var wg sync.WaitGroup
for _, url := range cacheURL {
go loop(wg, url)
}
for _, value := range results {
go loop(value)
}
wg.Wait()
}
func loop(value mongo.URL) {
func loop(wg sync.WaitGroup, url string) {
wg.Add(1)
defer wg.Done()
// Timeout 1 minute
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", value.URL, nil)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if checkErr(err, "new http request") {
Status.Error++
return

View File

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

View File

@ -5,6 +5,7 @@ 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

@ -15,6 +15,7 @@ type tomlConfig struct {
Backend backendConfig
AutoUpdate autoUpdateConfig
Cluster clusterConfig
MongoDB mongoDBConfig
}
type httpConfig struct {
@ -25,6 +26,7 @@ type httpConfig struct {
type backendConfig struct {
Enabled bool
Ping time.Duration
Cache int
}
type autoUpdateConfig struct {
@ -37,6 +39,11 @@ type clusterConfig struct {
Node int
}
type mongoDBConfig struct {
Database string
Collection string
}
var Toml tomlConfig
func init() {
@ -49,9 +56,22 @@ func init() {
if !common.CheckErr(err, "download default config") {
_, err = toml.DecodeFile("./config.toml", &Toml)
common.CheckErr(err, "decode toml config")
os.Exit(1)
}
}
}
if Toml.Backend.Cache == 0 {
Toml.Backend.Cache = 5
}
if Toml.MongoDB.Collection != "" {
Mongo_Collection = Toml.MongoDB.Collection
}
if Toml.MongoDB.Database != "" {
Mongo_DB = Toml.MongoDB.Database
}
}
func DownloadFile(filepath string, url string) error {

View File

@ -13,7 +13,7 @@ func Delete(url string) (*mongo.DeleteResult, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r, err := Coll.DeleteOne(ctx, bson.M{"_id": url})
r, err := Coll.DeleteOne(ctx, bson.M{"_id": url})
if err != nil {
fmt.Print(err)
return nil, err