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.

128 lines
3.7 KiB

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