37 lines
726 B
Go
37 lines
726 B
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Config struct {
|
|
SiteName string `json:"site_name"`
|
|
Port string `json:"port"`
|
|
DataDir string `json:"data_dir"`
|
|
}
|
|
|
|
func Load(path string) Config {
|
|
configData := Config{
|
|
SiteName: "Unnamed Download Listing",
|
|
Port: "8080",
|
|
DataDir: "data",
|
|
}
|
|
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
log.Println("No config file found, using defaults. Consider setting up a Site Name at the very least!")
|
|
return configData
|
|
}
|
|
defer file.Close()
|
|
|
|
// Overwrite the default configuration with data from 'file'
|
|
err = json.NewDecoder(file).Decode(&configData)
|
|
if err != nil {
|
|
log.Fatal("Config file exists but has errors:", err)
|
|
}
|
|
|
|
return configData
|
|
}
|