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.

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