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.

112 lines
3.4 KiB

6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
10 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "net/http"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. _ "github.com/chrislusf/seaweedfs/weed/filer2/cassandra"
  7. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  8. _ "github.com/chrislusf/seaweedfs/weed/filer2/memdb"
  9. _ "github.com/chrislusf/seaweedfs/weed/filer2/mysql"
  10. _ "github.com/chrislusf/seaweedfs/weed/filer2/postgres"
  11. _ "github.com/chrislusf/seaweedfs/weed/filer2/redis"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/notification"
  14. _ "github.com/chrislusf/seaweedfs/weed/notification/aws_sqs"
  15. _ "github.com/chrislusf/seaweedfs/weed/notification/google_pub_sub"
  16. _ "github.com/chrislusf/seaweedfs/weed/notification/kafka"
  17. _ "github.com/chrislusf/seaweedfs/weed/notification/log"
  18. "github.com/chrislusf/seaweedfs/weed/security"
  19. "github.com/spf13/viper"
  20. )
  21. type FilerOption struct {
  22. Masters []string
  23. Collection string
  24. DefaultReplication string
  25. RedirectOnRead bool
  26. DisableDirListing bool
  27. MaxMB int
  28. SecretKey string
  29. DirListingLimit int
  30. DataCenter string
  31. DefaultLevelDbDir string
  32. }
  33. type FilerServer struct {
  34. option *FilerOption
  35. secret security.Secret
  36. filer *filer2.Filer
  37. }
  38. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  39. fs = &FilerServer{
  40. option: option,
  41. }
  42. if len(option.Masters) == 0 {
  43. glog.Fatal("master list is required!")
  44. }
  45. fs.filer = filer2.NewFiler(option.Masters)
  46. go fs.filer.KeepConnectedToMaster()
  47. v := viper.GetViper()
  48. if !LoadConfiguration("filer", false) {
  49. v.Set("leveldb.enabled", true)
  50. v.Set("leveldb.dir", option.DefaultLevelDbDir)
  51. _, err := os.Stat(option.DefaultLevelDbDir)
  52. if os.IsNotExist(err) {
  53. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  54. }
  55. }
  56. LoadConfiguration("notification", false)
  57. fs.filer.LoadConfiguration(v)
  58. notification.LoadConfiguration(v.Sub("notification"))
  59. handleStaticResources(defaultMux)
  60. defaultMux.HandleFunc("/", fs.filerHandler)
  61. if defaultMux != readonlyMux {
  62. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  63. }
  64. return fs, nil
  65. }
  66. func (fs *FilerServer) jwt(fileId string) security.EncodedJwt {
  67. return security.GenJwt(fs.secret, fileId)
  68. }
  69. func LoadConfiguration(configFileName string, required bool) (loaded bool) {
  70. // find a filer store
  71. viper.SetConfigName(configFileName) // name of config file (without extension)
  72. viper.AddConfigPath(".") // optionally look for config in the working directory
  73. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  74. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  75. glog.V(0).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
  76. if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
  77. glog.V(0).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  78. if required {
  79. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  80. "\n\nPlease follow this example and add a filer.toml file to "+
  81. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n"+
  82. " https://github.com/chrislusf/seaweedfs/blob/master/weed/%s.toml\n"+
  83. "\nOr use this command to generate the default toml file\n"+
  84. " weed scaffold -config=%s -output=.\n\n\n",
  85. configFileName, configFileName, configFileName)
  86. } else {
  87. return false
  88. }
  89. }
  90. return true
  91. }