The backend for the AiteStory website
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.

63 lines
1.4 KiB

7 years ago
  1. package server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "os"
  6. "strings"
  7. )
  8. // Config is the struct created by the server's config.json
  9. type Config struct {
  10. View struct {
  11. Title string `json:"title"`
  12. } `json:"view"`
  13. DB struct {
  14. Username string `json:"username"`
  15. Password string `json:"password"`
  16. Database string `json:"database"`
  17. } `json:"db"`
  18. Server struct {
  19. Host string `json:"host"`
  20. Port int `json:"port"`
  21. UI string `json:"ui"`
  22. } `json:"server"`
  23. Wiki struct {
  24. URL string `json:"url"`
  25. Username string `json:"username"`
  26. Password string `json:"password"`
  27. } `json:"Wiki"`
  28. Test struct {
  29. Enabled bool `json:"enabled"`
  30. Username string `json:"username"`
  31. Password string `json:"password"`
  32. } `json:"Test"`
  33. }
  34. // Load loads the config file from the paths, starting with the first path.
  35. // If it fails to load any of them, it will return false.
  36. func (config *Config) Load(paths ...string) error {
  37. for _, path := range paths {
  38. // Open a file, or continue if it doesn't exist
  39. file, err := os.Open(path)
  40. if err != nil {
  41. continue
  42. }
  43. // JSON parsing errors should not cause it to skip to the next file.
  44. // That's given me enough grief in the past because JSON is a fickle
  45. // format for human-editable config files
  46. err = json.NewDecoder(file).Decode(&config)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. return errors.New("No configuration files found in either: " + strings.Join(paths, ","))
  53. }