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.

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