2021-07-29 18:51:15 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-08-07 20:03:26 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-07-29 18:51:15 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2021-08-12 19:26:54 +00:00
|
|
|
"github.com/MedzikUser/go-utils/common"
|
2021-07-29 18:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tomlConfig struct {
|
|
|
|
HTTP httpConfig
|
|
|
|
Backend backendConfig
|
|
|
|
AutoUpdate autoUpdateConfig
|
|
|
|
Cluster clusterConfig
|
2021-08-11 10:35:51 +00:00
|
|
|
MongoDB mongoDBConfig
|
2021-08-29 10:14:35 +00:00
|
|
|
Options optionsConfig
|
2021-07-29 18:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpConfig struct {
|
|
|
|
Enabled bool
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
type backendConfig struct {
|
|
|
|
Enabled bool
|
|
|
|
Ping time.Duration
|
2021-08-11 10:35:51 +00:00
|
|
|
Cache int
|
2021-07-29 18:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type autoUpdateConfig struct {
|
|
|
|
Enabled bool
|
|
|
|
Check time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type clusterConfig struct {
|
|
|
|
ID int
|
|
|
|
Node int
|
|
|
|
}
|
|
|
|
|
2021-08-11 10:35:51 +00:00
|
|
|
type mongoDBConfig struct {
|
|
|
|
Database string
|
|
|
|
Collection string
|
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:35 +00:00
|
|
|
type optionsConfig struct {
|
2021-08-30 13:41:06 +00:00
|
|
|
Stop_After_Ping bool
|
|
|
|
Stop_After_Ping_Timeout time.Duration
|
2021-08-29 10:14:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 18:51:15 +00:00
|
|
|
var Toml tomlConfig
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
_, err := toml.DecodeFile("./config.toml", &Toml)
|
|
|
|
|
2021-08-07 20:03:26 +00:00
|
|
|
if common.CheckErr(err, "decode toml config") {
|
|
|
|
if err.Error() == "open ./config.toml: no such file or directory" {
|
|
|
|
err := DownloadFile("config.toml", "https://github.com/"+GH_Repo+"/raw/main/config.schema.toml")
|
|
|
|
|
|
|
|
if !common.CheckErr(err, "download default config") {
|
|
|
|
_, err = toml.DecodeFile("./config.toml", &Toml)
|
|
|
|
common.CheckErr(err, "decode toml config")
|
2021-08-11 10:35:51 +00:00
|
|
|
os.Exit(1)
|
2021-08-07 20:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 10:35:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-08-07 20:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadFile(filepath string, url string) error {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
2021-07-29 18:51:15 +00:00
|
|
|
}
|