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.

76 lines
1.5 KiB

  1. package config
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "os"
  7. "sync"
  8. )
  9. var globalMutex sync.Mutex
  10. var global *Config
  11. // Config is configuration
  12. type Config struct {
  13. Space struct {
  14. Enabled bool `json:"enabled"`
  15. Host string `json:"host"`
  16. AccessKey string `json:"accessKey"`
  17. SecretKey string `json:"secretKey"`
  18. Bucket string `json:"bucket"`
  19. MaxSize int64 `json:"maxSize"`
  20. Root string `json:"root"`
  21. } `json:"space"`
  22. Database struct {
  23. Host string `json:"host"`
  24. Port int `json:"port"`
  25. Db string `json:"db"`
  26. Username string `json:"username"`
  27. Password string `json:"password"`
  28. Mechanism string `json:"mechanism"`
  29. } `json:"database"`
  30. Wiki struct {
  31. URL string `json:"url"`
  32. } `json:"wiki"`
  33. }
  34. // Load loads config stuff
  35. func (config *Config) Load(filename string) error {
  36. file, err := os.Open(filename)
  37. if err != nil {
  38. return err
  39. }
  40. return json.NewDecoder(file).Decode(config)
  41. }
  42. // LoadAny loads the first of these files it can find
  43. func (config *Config) LoadAny(filenames ...string) error {
  44. for _, filename := range filenames {
  45. if err := config.Load(filename); err == nil {
  46. return nil
  47. }
  48. *config = Config{}
  49. }
  50. return errors.New("Failed to load configuration files")
  51. }
  52. // Global gets the global configuration, loading it if this is the first caller
  53. func Global() Config {
  54. globalMutex.Lock()
  55. if global == nil {
  56. global = &Config{}
  57. err := global.LoadAny("/etc/aiterp/rpdata.json", "./config.json")
  58. if err != nil {
  59. log.Fatalln(err)
  60. }
  61. }
  62. globalMutex.Unlock()
  63. return *global
  64. }