mirror of https://github.com/MedzikUser/go-pingbot
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:
parent
f09209d1bf
commit
3432a74d23
|
@ -1,4 +1,2 @@
|
||||||
# MongoDB
|
# MongoDB
|
||||||
MONGODB_URI=mongodb+srv://...
|
MONGODB_URI=mongodb+srv://...
|
||||||
MONGODB_DB=...
|
|
||||||
MONGODB_COLLECTION=...
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,31 +3,43 @@ package backend
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitlab.com/gaming0skar123/go/pingbot/common"
|
"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() {
|
func ping() {
|
||||||
results, err := mongo.GetAll()
|
if cacheRetry >= config.Toml.Backend.Cache {
|
||||||
if checkErr(err, "get keys from db") {
|
cache()
|
||||||
return
|
cacheRetry = 0
|
||||||
|
}
|
||||||
|
cacheRetry++
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
for _, url := range cacheURL {
|
||||||
|
go loop(wg, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, value := range results {
|
wg.Wait()
|
||||||
go loop(value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loop(value mongo.URL) {
|
func loop(wg sync.WaitGroup, url string) {
|
||||||
|
wg.Add(1)
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
// Timeout 1 minute
|
// Timeout 1 minute
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
||||||
defer cancel()
|
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") {
|
if checkErr(err, "new http request") {
|
||||||
Status.Error++
|
Status.Error++
|
||||||
return
|
return
|
||||||
|
|
|
@ -7,7 +7,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Ticker() {
|
func Ticker() {
|
||||||
// Ping on Start
|
// On Start
|
||||||
|
cache()
|
||||||
ping()
|
ping()
|
||||||
|
|
||||||
ticker := time.NewTicker(config.Toml.Backend.Ping * time.Minute)
|
ticker := time.NewTicker(config.Toml.Backend.Ping * time.Minute)
|
||||||
|
|
|
@ -5,6 +5,7 @@ port=8080 # Port to listen
|
||||||
[backend]
|
[backend]
|
||||||
enabled=true # Enable Backend
|
enabled=true # Enable Backend
|
||||||
ping=3 # Ping every three minutes
|
ping=3 # Ping every three minutes
|
||||||
|
cache=5 # Get urls from db every five url pings
|
||||||
|
|
||||||
[autoupdate]
|
[autoupdate]
|
||||||
enabled=true # Enable Auto Updater
|
enabled=true # Enable Auto Updater
|
||||||
|
|
|
@ -15,6 +15,7 @@ type tomlConfig struct {
|
||||||
Backend backendConfig
|
Backend backendConfig
|
||||||
AutoUpdate autoUpdateConfig
|
AutoUpdate autoUpdateConfig
|
||||||
Cluster clusterConfig
|
Cluster clusterConfig
|
||||||
|
MongoDB mongoDBConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpConfig struct {
|
type httpConfig struct {
|
||||||
|
@ -25,6 +26,7 @@ type httpConfig struct {
|
||||||
type backendConfig struct {
|
type backendConfig struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Ping time.Duration
|
Ping time.Duration
|
||||||
|
Cache int
|
||||||
}
|
}
|
||||||
|
|
||||||
type autoUpdateConfig struct {
|
type autoUpdateConfig struct {
|
||||||
|
@ -37,6 +39,11 @@ type clusterConfig struct {
|
||||||
Node int
|
Node int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mongoDBConfig struct {
|
||||||
|
Database string
|
||||||
|
Collection string
|
||||||
|
}
|
||||||
|
|
||||||
var Toml tomlConfig
|
var Toml tomlConfig
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -49,9 +56,22 @@ func init() {
|
||||||
if !common.CheckErr(err, "download default config") {
|
if !common.CheckErr(err, "download default config") {
|
||||||
_, err = toml.DecodeFile("./config.toml", &Toml)
|
_, err = toml.DecodeFile("./config.toml", &Toml)
|
||||||
common.CheckErr(err, "decode toml config")
|
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 {
|
func DownloadFile(filepath string, url string) error {
|
||||||
|
|
|
@ -13,7 +13,7 @@ func Delete(url string) (*mongo.DeleteResult, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
r, err := Coll.DeleteOne(ctx, bson.M{"_id": url})
|
r, err := Coll.DeleteOne(ctx, bson.M{"_id": url})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print(err)
|
fmt.Print(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue