package server import ( "encoding/json" "errors" "os" "strings" ) // Config is the struct created by the server's config.json type Config struct { View struct { Title string `json:"title"` } `json:"view"` DB struct { Username string `json:"username"` Password string `json:"password"` Database string `json:"database"` } `json:"db"` Server struct { Host string `json:"host"` Port int `json:"port"` UI string `json:"ui"` } `json:"server"` Wiki struct { URL string `json:"url"` Username string `json:"username"` Password string `json:"password"` } `json:"Wiki"` Test struct { Enabled bool `json:"enabled"` Username string `json:"username"` Password string `json:"password"` } `json:"Test"` } // Load loads the config file from the paths, starting with the first path. // If it fails to load any of them, it will return false. func (config *Config) Load(paths ...string) error { for _, path := range paths { // Open a file, or continue if it doesn't exist file, err := os.Open(path) if err != nil { continue } // JSON parsing errors should not cause it to skip to the next file. // That's given me enough grief in the past because JSON is a fickle // format for human-editable config files err = json.NewDecoder(file).Decode(&config) if err != nil { return err } return nil } return errors.New("No configuration files found in either: " + strings.Join(paths, ",")) }