GraphQL API and utilities for the rpdata project
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.

104 lines
2.4 KiB

  1. package config
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "os"
  7. "strings"
  8. "sync"
  9. "gopkg.in/yaml.v2"
  10. )
  11. var globalMutex sync.Mutex
  12. var global *Config
  13. // Config is configuration data
  14. type Config struct {
  15. Space Space `json:"space" yaml:"space"`
  16. Database Database `json:"database" yaml:"database"`
  17. Wiki Wiki `json:"wiki" yaml:"wiki"`
  18. }
  19. // Space is configuration for spaces.
  20. type Space struct {
  21. Enabled bool `json:"enabled" yaml:"enabled"`
  22. Host string `json:"host" yaml:"host"`
  23. AccessKey string `json:"accessKey" yaml:"accessKey"`
  24. SecretKey string `json:"secretKey" yaml:"secretKey"`
  25. Bucket string `json:"bucket" yaml:"bucket"`
  26. MaxSize int64 `json:"maxSize" yaml:"maxSize"`
  27. Root string `json:"root" yaml:"root"`
  28. URLRoot string `json:"urlRoot" yaml:"urlRoot"`
  29. }
  30. // Database is configuration for spaces.
  31. type Database struct {
  32. Driver string `json:"driver" yaml:"driver"`
  33. Host string `json:"host" yaml:"host"`
  34. Port int `json:"port" yaml:"port"`
  35. Db string `json:"db" yaml:"db"`
  36. Username string `json:"username" yaml:"username"`
  37. Password string `json:"password" yaml:"password"`
  38. Mechanism string `json:"mechanism" yaml:"mechanism"`
  39. RestoreIDs bool `json:"restoreIDs" yaml:"restoreIDs"`
  40. }
  41. // Wiki is the wiki stuff.
  42. type Wiki struct {
  43. URL string `json:"url" yaml:"url"`
  44. }
  45. // Load loads config stuff
  46. func (config *Config) Load(filename string) error {
  47. log.Println("Trying to load config from " + filename)
  48. stat, err := os.Stat(filename)
  49. if err != nil {
  50. return err
  51. }
  52. if stat.Size() < 1 {
  53. return errors.New("File is empty")
  54. }
  55. file, err := os.Open(filename)
  56. if err != nil {
  57. return err
  58. }
  59. if strings.HasSuffix(filename, ".json") {
  60. return json.NewDecoder(file).Decode(config)
  61. }
  62. return yaml.NewDecoder(file).Decode(config)
  63. }
  64. // LoadAny loads the first of these files it can find
  65. func (config *Config) LoadAny(filenames ...string) error {
  66. for _, filename := range filenames {
  67. if err := config.Load(filename); err != nil {
  68. continue
  69. }
  70. return nil
  71. }
  72. return errors.New("Failed to load configuration files")
  73. }
  74. // Global gets the global configuration, loading it if this is the first caller
  75. func Global() Config {
  76. globalMutex.Lock()
  77. if global == nil {
  78. global = &Config{}
  79. err := global.LoadAny("/etc/aiterp/rpdata.yaml", "/etc/aiterp/rpdata.json", "./config.yaml", "./config.json")
  80. if err != nil {
  81. log.Fatalln(err)
  82. }
  83. }
  84. globalMutex.Unlock()
  85. return *global
  86. }