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.

94 lines
2.5 KiB

  1. package filer2
  2. import (
  3. "os"
  4. "github.com/spf13/viper"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. const (
  8. FILER_TOML_EXAMPLE = `
  9. # A sample TOML config file for SeaweedFS filer store
  10. # local in memory, mostly for testing purpose
  11. [memory]
  12. enabled = false
  13. [leveldb]
  14. enabled = false
  15. dir = "." # directory to store level db files
  16. [mysql]
  17. # also need to manually create seaweedfs table.
  18. # CREATE TABLE IF NOT EXISTS seaweedfs (
  19. # directory VARCHAR(4096) NOT NULL DEFAULT "" COMMENT 'full path to parent directory',
  20. # name VARCHAR(1024) NOT NULL DEFAULT "" COMMENT 'directory or file name',
  21. # meta BLOB,
  22. # PRIMARY KEY (directory, name),
  23. # ) DEFAULT CHARSET=utf8;
  24. enabled = true
  25. server = "localhost"
  26. port = 3306
  27. username = "root"
  28. password = ""
  29. database = ""
  30. connection_max_idle = 2
  31. connection_max_open = 100
  32. [postgres]
  33. enabled = false
  34. server = "192.168.1.1"
  35. port = 8080
  36. username = ""
  37. password = ""
  38. database = ""
  39. connection_max_idle = 100
  40. connection_max_open = 100
  41. `
  42. )
  43. var (
  44. Stores []FilerStore
  45. )
  46. func (f *Filer) LoadConfiguration() {
  47. // find a filer store
  48. viper.SetConfigName("filer") // name of config file (without extension)
  49. viper.AddConfigPath(".") // optionally look for config in the working directory
  50. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  51. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  52. if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
  53. glog.Fatalf("Failed to load filer.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/" +
  54. "\n\nPlease follow this example and add a filer.toml file to " +
  55. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n" + FILER_TOML_EXAMPLE)
  56. }
  57. glog.V(0).Infof("Reading filer configuration from %s", viper.ConfigFileUsed())
  58. for _, store := range Stores {
  59. if viper.GetBool(store.GetName() + ".enabled") {
  60. viperSub := viper.Sub(store.GetName())
  61. if err := store.Initialize(viperSub); err != nil {
  62. glog.Fatalf("Failed to initialize store for %s: %+v",
  63. store.GetName(), err)
  64. }
  65. f.SetStore(store)
  66. glog.V(0).Infof("Configure filer for %s from %s", store.GetName(), viper.ConfigFileUsed())
  67. return
  68. }
  69. }
  70. println()
  71. println("Supported filer stores are:")
  72. for _, store := range Stores {
  73. println(" " + store.GetName())
  74. }
  75. println()
  76. println("Please configure a supported filer store in", viper.ConfigFileUsed())
  77. println()
  78. os.Exit(-1)
  79. }