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.

105 lines
2.5 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. SSL bool `json:"ssl" yaml:"ssl"`
  41. }
  42. // Wiki is the wiki stuff.
  43. type Wiki struct {
  44. URL string `json:"url" yaml:"url"`
  45. }
  46. // Load loads config stuff
  47. func (config *Config) Load(filename string) error {
  48. log.Println("Trying to load config from " + filename)
  49. stat, err := os.Stat(filename)
  50. if err != nil {
  51. return err
  52. }
  53. if stat.Size() < 1 {
  54. return errors.New("File is empty")
  55. }
  56. file, err := os.Open(filename)
  57. if err != nil {
  58. return err
  59. }
  60. if strings.HasSuffix(filename, ".json") {
  61. return json.NewDecoder(file).Decode(config)
  62. }
  63. return yaml.NewDecoder(file).Decode(config)
  64. }
  65. // LoadAny loads the first of these files it can find
  66. func (config *Config) LoadAny(filenames ...string) error {
  67. for _, filename := range filenames {
  68. if err := config.Load(filename); err != nil {
  69. continue
  70. }
  71. return nil
  72. }
  73. return errors.New("Failed to load configuration files")
  74. }
  75. // Global gets the global configuration, loading it if this is the first caller
  76. func Global() Config {
  77. globalMutex.Lock()
  78. if global == nil {
  79. global = &Config{}
  80. err := global.LoadAny("/etc/aiterp/rpdata.yaml", "/etc/aiterp/rpdata.json", "./config.yaml", "./config.json")
  81. if err != nil {
  82. log.Fatalln(err)
  83. }
  84. }
  85. globalMutex.Unlock()
  86. return *global
  87. }