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.

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