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.

151 lines
5.1 KiB

11 years ago
7 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. "time"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  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. "strings"
  13. )
  14. var (
  15. f FilerOptions
  16. )
  17. type FilerOptions struct {
  18. masters *string
  19. ip *string
  20. port *int
  21. grpcPort *int
  22. publicPort *int
  23. collection *string
  24. defaultReplicaPlacement *string
  25. redirectOnRead *bool
  26. disableDirListing *bool
  27. maxMB *int
  28. secretKey *string
  29. dirListingLimit *int
  30. dataCenter *string
  31. enableNotification *bool
  32. }
  33. func init() {
  34. cmdFiler.Run = runFiler // break init cycle
  35. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  36. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  37. f.ip = cmdFiler.Flag.String("ip", "", "filer server http listen ip address")
  38. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  39. f.grpcPort = cmdFiler.Flag.Int("port.grpc", 0, "filer grpc server listen port, default to http port + 10000")
  40. f.publicPort = cmdFiler.Flag.Int("port.public", 0, "port opened to public")
  41. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "000", "default replication type if not specified")
  42. f.redirectOnRead = cmdFiler.Flag.Bool("redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  43. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  44. f.maxMB = cmdFiler.Flag.Int("maxMB", 32, "split files larger than the limit")
  45. f.secretKey = cmdFiler.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  46. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 1000, "limit sub dir listing size")
  47. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to write to volumes in this data center")
  48. f.enableNotification = cmdFiler.Flag.Bool("notify", false, "send file updates to the queue defined in message_queue.toml")
  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 following are example filer.toml configuration file.
  64. ` + filer2.FILER_TOML_EXAMPLE + "\n",
  65. }
  66. func runFiler(cmd *Command, args []string) bool {
  67. f.start()
  68. return true
  69. }
  70. func (fo *FilerOptions) start() {
  71. defaultMux := http.NewServeMux()
  72. publicVolumeMux := defaultMux
  73. if *fo.publicPort != 0 {
  74. publicVolumeMux = http.NewServeMux()
  75. }
  76. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  77. Masters: strings.Split(*f.masters, ","),
  78. Collection: *fo.collection,
  79. DefaultReplication: *fo.defaultReplicaPlacement,
  80. RedirectOnRead: *fo.redirectOnRead,
  81. DisableDirListing: *fo.disableDirListing,
  82. MaxMB: *fo.maxMB,
  83. SecretKey: *fo.secretKey,
  84. DirListingLimit: *fo.dirListingLimit,
  85. DataCenter: *fo.dataCenter,
  86. EnableNotification: *fo.enableNotification,
  87. })
  88. if nfs_err != nil {
  89. glog.Fatalf("Filer startup error: %v", nfs_err)
  90. }
  91. if *fo.publicPort != 0 {
  92. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  93. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  94. publicListener, e := util.NewListener(publicListeningAddress, 0)
  95. if e != nil {
  96. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  97. }
  98. go func() {
  99. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  100. glog.Fatalf("Volume server fail to serve public: %v", e)
  101. }
  102. }()
  103. }
  104. glog.V(0).Infoln("Start Seaweed Filer", util.VERSION, "at port", strconv.Itoa(*fo.port))
  105. filerListener, e := util.NewListener(
  106. ":"+strconv.Itoa(*fo.port),
  107. time.Duration(10)*time.Second,
  108. )
  109. if e != nil {
  110. glog.Fatalf("Filer listener error: %v", e)
  111. }
  112. // starting grpc server
  113. grpcPort := *fo.grpcPort
  114. if grpcPort == 0 {
  115. grpcPort = *fo.port + 10000
  116. }
  117. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  118. if err != nil {
  119. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  120. }
  121. grpcS := util.NewGrpcServer()
  122. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  123. reflection.Register(grpcS)
  124. go grpcS.Serve(grpcL)
  125. httpS := &http.Server{Handler: defaultMux}
  126. if err := httpS.Serve(filerListener); err != nil {
  127. glog.Fatalf("Filer Fail to serve: %v", e)
  128. }
  129. }