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.

157 lines
5.2 KiB

11 years ago
6 years ago
11 years ago
7 years ago
7 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/server"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "google.golang.org/grpc/reflection"
  12. )
  13. var (
  14. f FilerOptions
  15. )
  16. type FilerOptions struct {
  17. masters *string
  18. ip *string
  19. port *int
  20. grpcPort *int
  21. publicPort *int
  22. collection *string
  23. defaultReplicaPlacement *string
  24. redirectOnRead *bool
  25. disableDirListing *bool
  26. maxMB *int
  27. secretKey *string
  28. dirListingLimit *int
  29. dataCenter *string
  30. enableNotification *bool
  31. // default leveldb directory, used in "weed server" mode
  32. defaultLevelDbDirectory *string
  33. }
  34. func init() {
  35. cmdFiler.Run = runFiler // break init cycle
  36. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  37. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  38. f.ip = cmdFiler.Flag.String("ip", "", "filer server http listen ip address")
  39. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  40. f.grpcPort = cmdFiler.Flag.Int("port.grpc", 0, "filer grpc server listen port, default to http port + 10000")
  41. f.publicPort = cmdFiler.Flag.Int("port.public", 0, "port opened to public")
  42. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "000", "default replication type if not specified")
  43. f.redirectOnRead = cmdFiler.Flag.Bool("redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  44. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  45. f.maxMB = cmdFiler.Flag.Int("maxMB", 32, "split files larger than the limit")
  46. f.secretKey = cmdFiler.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  47. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 100000, "limit sub dir listing size")
  48. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to write to volumes in this data center")
  49. }
  50. var cmdFiler = &Command{
  51. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  52. Short: "start a file server that points to a master server, or a list of master servers",
  53. Long: `start a file server which accepts REST operation for any files.
  54. //create or overwrite the file, the directories /path/to will be automatically created
  55. POST /path/to/file
  56. //get the file content
  57. GET /path/to/file
  58. //create or overwrite the file, the filename in the multipart request will be used
  59. POST /path/to/
  60. //return a json format subdirectory and files listing
  61. GET /path/to/
  62. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  63. The example filer.toml configuration file can be generated by "weed scaffold filer"
  64. `,
  65. }
  66. func runFiler(cmd *Command, args []string) bool {
  67. f.startFiler()
  68. return true
  69. }
  70. func (fo *FilerOptions) startFiler() {
  71. defaultMux := http.NewServeMux()
  72. publicVolumeMux := defaultMux
  73. if *fo.publicPort != 0 {
  74. publicVolumeMux = http.NewServeMux()
  75. }
  76. defaultLevelDbDirectory := "./filerdb"
  77. if fo.defaultLevelDbDirectory != nil {
  78. defaultLevelDbDirectory = *fo.defaultLevelDbDirectory + "/filerdb"
  79. }
  80. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  81. Masters: strings.Split(*f.masters, ","),
  82. Collection: *fo.collection,
  83. DefaultReplication: *fo.defaultReplicaPlacement,
  84. RedirectOnRead: *fo.redirectOnRead,
  85. DisableDirListing: *fo.disableDirListing,
  86. MaxMB: *fo.maxMB,
  87. SecretKey: *fo.secretKey,
  88. DirListingLimit: *fo.dirListingLimit,
  89. DataCenter: *fo.dataCenter,
  90. DefaultLevelDbDir: defaultLevelDbDirectory,
  91. })
  92. if nfs_err != nil {
  93. glog.Fatalf("Filer startup error: %v", nfs_err)
  94. }
  95. if *fo.publicPort != 0 {
  96. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  97. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  98. publicListener, e := util.NewListener(publicListeningAddress, 0)
  99. if e != nil {
  100. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  101. }
  102. go func() {
  103. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  104. glog.Fatalf("Volume server fail to serve public: %v", e)
  105. }
  106. }()
  107. }
  108. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.VERSION, *fo.ip, *fo.port)
  109. filerListener, e := util.NewListener(
  110. ":"+strconv.Itoa(*fo.port),
  111. time.Duration(10)*time.Second,
  112. )
  113. if e != nil {
  114. glog.Fatalf("Filer listener error: %v", e)
  115. }
  116. // starting grpc server
  117. grpcPort := *fo.grpcPort
  118. if grpcPort == 0 {
  119. grpcPort = *fo.port + 10000
  120. }
  121. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  122. if err != nil {
  123. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  124. }
  125. grpcS := util.NewGrpcServer()
  126. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  127. reflection.Register(grpcS)
  128. go grpcS.Serve(grpcL)
  129. httpS := &http.Server{Handler: defaultMux}
  130. if err := httpS.Serve(filerListener); err != nil {
  131. glog.Fatalf("Filer Fail to serve: %v", e)
  132. }
  133. }