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.

111 lines
3.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
10 years ago
7 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. DirListingLimit int
  29. DataCenter string
  30. DefaultLevelDbDir string
  31. }
  32. type FilerServer struct {
  33. option *FilerOption
  34. secret security.SigningKey
  35. filer *filer2.Filer
  36. }
  37. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  38. fs = &FilerServer{
  39. option: option,
  40. }
  41. if len(option.Masters) == 0 {
  42. glog.Fatal("master list is required!")
  43. }
  44. fs.filer = filer2.NewFiler(option.Masters)
  45. go fs.filer.KeepConnectedToMaster()
  46. v := viper.GetViper()
  47. if !LoadConfiguration("filer", false) {
  48. v.Set("leveldb.enabled", true)
  49. v.Set("leveldb.dir", option.DefaultLevelDbDir)
  50. _, err := os.Stat(option.DefaultLevelDbDir)
  51. if os.IsNotExist(err) {
  52. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  53. }
  54. }
  55. LoadConfiguration("notification", false)
  56. fs.filer.LoadConfiguration(v)
  57. notification.LoadConfiguration(v.Sub("notification"))
  58. handleStaticResources(defaultMux)
  59. defaultMux.HandleFunc("/", fs.filerHandler)
  60. if defaultMux != readonlyMux {
  61. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  62. }
  63. return fs, nil
  64. }
  65. func (fs *FilerServer) jwt(fileId string) security.EncodedJwt {
  66. return security.GenJwt(fs.secret, fileId)
  67. }
  68. func LoadConfiguration(configFileName string, required bool) (loaded bool) {
  69. // find a filer store
  70. viper.SetConfigName(configFileName) // name of config file (without extension)
  71. viper.AddConfigPath(".") // optionally look for config in the working directory
  72. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  73. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  74. glog.V(0).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
  75. if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
  76. glog.V(0).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  77. if required {
  78. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  79. "\n\nPlease follow this example and add a filer.toml file to "+
  80. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n"+
  81. " https://github.com/chrislusf/seaweedfs/blob/master/weed/%s.toml\n"+
  82. "\nOr use this command to generate the default toml file\n"+
  83. " weed scaffold -config=%s -output=.\n\n\n",
  84. configFileName, configFileName, configFileName)
  85. } else {
  86. return false
  87. }
  88. }
  89. return true
  90. }