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.

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