You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.5 KiB
68 lines
1.5 KiB
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"`
|
|
Debug bool `json:"debug"`
|
|
} `json:"server"`
|
|
|
|
Directories struct {
|
|
UI string `json:"ui"`
|
|
Templates string `json:"templates"`
|
|
} `json:"directories"`
|
|
|
|
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, ","))
|
|
}
|