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.

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