feat(config): auto download schema config

This commit is contained in:
Medzik 2021-08-07 20:03:26 +00:00
parent 683aad93fe
commit fe16a2ab62
2 changed files with 43 additions and 11 deletions

View File

@ -1,19 +1,19 @@
[http]
enabled=true # Enable Website
port=8080 # Port to listen
enabled=true # Enable Website
port=8080 # Port to listen
[backend]
enabled=true # Enable Backend
ping=3 # Ping every three minutes
enabled=true # Enable Backend
ping=3 # Ping every three minutes
[autoupdate]
enabled=true # Enable Auto Updater
check=2 # Check every two minutes
enabled=true # Enable Auto Updater
check=2 # Check every two minutes
[cluster]
id=1 # Cluster ID
node=1 # Node ID
id=1 # Cluster ID
node=1 # Node ID
[mongodb]
database="PingBot"
collection="URL"
database="PingBot" # MongoDB Database Name
collection="URL" # MongoDB Collection Name

View File

@ -1,6 +1,9 @@
package config
import (
"io"
"net/http"
"os"
"time"
"github.com/BurntSushi/toml"
@ -39,5 +42,34 @@ var Toml tomlConfig
func init() {
_, err := toml.DecodeFile("./config.toml", &Toml)
common.CheckErr(err, "decode toml config")
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")
}
}
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
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
}