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.

82 lines
2.1 KiB

7 years ago
  1. package msgqueue
  2. import (
  3. "os"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/spf13/viper"
  6. )
  7. const (
  8. MSG_QUEUE_TOML_EXAMPLE = `
  9. # A sample TOML config file for SeaweedFS message queue
  10. [log]
  11. enabled = true
  12. [kafka]
  13. enabled = false
  14. hosts = [
  15. "localhost:9092"
  16. ]
  17. topic = "seaweedfs_filer"
  18. `
  19. )
  20. var (
  21. MessageQueues []MessageQueue
  22. Queue MessageQueue
  23. )
  24. func LoadConfiguration() {
  25. // find a filer store
  26. viper.SetConfigName("message_queue") // name of config file (without extension)
  27. viper.AddConfigPath(".") // optionally look for config in the working directory
  28. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  29. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  30. if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
  31. glog.Fatalf("Failed to load message_queue.toml file from current directory, or $HOME/.seaweedfs/, "+
  32. "or /etc/seaweedfs/"+
  33. "\n\nPlease follow this example and add a message_queue.toml file to "+
  34. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n"+MSG_QUEUE_TOML_EXAMPLE, err)
  35. }
  36. glog.V(0).Infof("Reading message queue configuration from %s", viper.ConfigFileUsed())
  37. for _, store := range MessageQueues {
  38. if viper.GetBool(store.GetName() + ".enabled") {
  39. viperSub := viper.Sub(store.GetName())
  40. if err := store.Initialize(viperSub); err != nil {
  41. glog.Fatalf("Failed to initialize store for %s: %+v",
  42. store.GetName(), err)
  43. }
  44. Queue = store
  45. glog.V(0).Infof("Configure message queue for %s from %s", store.GetName(), viper.ConfigFileUsed())
  46. return
  47. }
  48. }
  49. println()
  50. println("Supported message queues are:")
  51. for _, store := range MessageQueues {
  52. println(" " + store.GetName())
  53. }
  54. println()
  55. println("Please configure a supported message queue in", viper.ConfigFileUsed())
  56. println()
  57. os.Exit(-1)
  58. }
  59. // A simplified interface to decouple from Viper
  60. type Configuration interface {
  61. GetString(key string) string
  62. GetBool(key string) bool
  63. GetInt(key string) int
  64. GetInt64(key string) int64
  65. GetFloat64(key string) float64
  66. GetStringSlice(key string) []string
  67. }