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.

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