diff --git a/config.schema.toml b/config.schema.toml index a19ef69..7e7a1a0 100644 --- a/config.schema.toml +++ b/config.schema.toml @@ -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 diff --git a/config/toml.go b/config/toml.go index d54dd13..8f65483 100644 --- a/config/toml.go +++ b/config/toml.go @@ -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 }