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.

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