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.

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