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.

140 lines
4.3 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. "github.com/soheilhy/cmux"
  12. "google.golang.org/grpc"
  13. "google.golang.org/grpc/reflection"
  14. "strings"
  15. )
  16. var (
  17. f FilerOptions
  18. )
  19. type FilerOptions struct {
  20. masters *string
  21. ip *string
  22. port *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.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. m := cmux.New(filerListener)
  102. grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
  103. httpL := m.Match(cmux.Any())
  104. // Create your protocol servers.
  105. grpcS := grpc.NewServer()
  106. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  107. reflection.Register(grpcS)
  108. httpS := &http.Server{Handler: defaultMux}
  109. go grpcS.Serve(grpcL)
  110. go httpS.Serve(httpL)
  111. if err := m.Serve(); err != nil {
  112. glog.Fatalf("Filer Fail to serve: %v", e)
  113. }
  114. }