mirror of https://github.com/MedzikUser/go-pingbot
feat(config): auto download schema config
This commit is contained in:
parent
683aad93fe
commit
fe16a2ab62
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue