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.

68 lines
1.5 KiB

7 years ago
7 years ago
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. Debug bool `json:"debug"`
  22. } `json:"server"`
  23. Directories struct {
  24. UI string `json:"ui"`
  25. Templates string `json:"templates"`
  26. } `json:"directories"`
  27. Wiki struct {
  28. URL string `json:"url"`
  29. Username string `json:"username"`
  30. Password string `json:"password"`
  31. } `json:"Wiki"`
  32. Test struct {
  33. Enabled bool `json:"enabled"`
  34. Username string `json:"username"`
  35. Password string `json:"password"`
  36. } `json:"Test"`
  37. }
  38. // Load loads the config file from the paths, starting with the first path.
  39. // If it fails to load any of them, it will return false.
  40. func (config *Config) Load(paths ...string) error {
  41. for _, path := range paths {
  42. // Open a file, or continue if it doesn't exist
  43. file, err := os.Open(path)
  44. if err != nil {
  45. continue
  46. }
  47. // JSON parsing errors should not cause it to skip to the next file.
  48. // That's given me enough grief in the past because JSON is a fickle
  49. // format for human-editable config files
  50. err = json.NewDecoder(file).Decode(&config)
  51. if err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. return errors.New("No configuration files found in either: " + strings.Join(paths, ","))
  57. }