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.

99 lines
3.1 KiB

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