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.

95 lines
3.0 KiB

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