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] [http]
enabled=true # Enable Website enabled=true # Enable Website
port=8080 # Port to listen 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
[autoupdate] [autoupdate]
enabled=true # Enable Auto Updater enabled=true # Enable Auto Updater
check=2 # Check every two minutes check=2 # Check every two minutes
[cluster] [cluster]
id=1 # Cluster ID id=1 # Cluster ID
node=1 # Node ID node=1 # Node ID
[mongodb] [mongodb]
database="PingBot" database="PingBot" # MongoDB Database Name
collection="URL" collection="URL" # MongoDB Collection Name

View File

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