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.

101 lines
2.8 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. [memory]
  11. # local in memory, mostly for testing purpose
  12. enabled = false
  13. [leveldb]
  14. # local on disk, mostly for simple single-machine setup, fairly scalable
  15. enabled = false
  16. dir = "." # directory to store level db files
  17. [mysql]
  18. # multiple filers on shared storage, fairly scalable
  19. #
  20. # need to choose or create a database.
  21. # need to manually create a table "filemeta".
  22. #
  23. # CREATE TABLE IF NOT EXISTS filemeta (
  24. # dirhash BINARY(16) COMMENT 'MD5 hash value of directory field',
  25. # name VARCHAR(1000) NOT NULL DEFAULT "" COMMENT 'directory or file name',
  26. # directory VARCHAR(4096) NOT NULL DEFAULT "" COMMENT 'full path to parent directory',
  27. # meta BLOB,
  28. # PRIMARY KEY (dirhash, name)
  29. # ) DEFAULT CHARSET=utf8;
  30. #
  31. enabled = true
  32. server = "localhost"
  33. port = 3306
  34. username = "root"
  35. password = ""
  36. database = "" # create or use an existing database, create the seaweedfs table.
  37. connection_max_idle = 2
  38. connection_max_open = 100
  39. [postgres]
  40. enabled = false
  41. server = "192.168.1.1"
  42. port = 8080
  43. username = ""
  44. password = ""
  45. database = ""
  46. connection_max_idle = 100
  47. connection_max_open = 100
  48. `
  49. )
  50. var (
  51. Stores []FilerStore
  52. )
  53. func (f *Filer) LoadConfiguration() {
  54. // find a filer store
  55. viper.SetConfigName("filer") // name of config file (without extension)
  56. viper.AddConfigPath(".") // optionally look for config in the working directory
  57. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  58. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  59. if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
  60. glog.Fatalf("Failed to load filer.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/" +
  61. "\n\nPlease follow this example and add a filer.toml file to " +
  62. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n" + FILER_TOML_EXAMPLE)
  63. }
  64. glog.V(0).Infof("Reading filer configuration from %s", viper.ConfigFileUsed())
  65. for _, store := range Stores {
  66. if viper.GetBool(store.GetName() + ".enabled") {
  67. viperSub := viper.Sub(store.GetName())
  68. if err := store.Initialize(viperSub); err != nil {
  69. glog.Fatalf("Failed to initialize store for %s: %+v",
  70. store.GetName(), err)
  71. }
  72. f.SetStore(store)
  73. glog.V(0).Infof("Configure filer for %s from %s", store.GetName(), viper.ConfigFileUsed())
  74. return
  75. }
  76. }
  77. println()
  78. println("Supported filer stores are:")
  79. for _, store := range Stores {
  80. println(" " + store.GetName())
  81. }
  82. println()
  83. println("Please configure a supported filer store in", viper.ConfigFileUsed())
  84. println()
  85. os.Exit(-1)
  86. }