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.

148 lines
4.9 KiB

11 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/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/server"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "google.golang.org/grpc/reflection"
  11. "strings"
  12. )
  13. var (
  14. f FilerOptions
  15. )
  16. type FilerOptions struct {
  17. masters *string
  18. ip *string
  19. port *int
  20. grpcPort *int
  21. publicPort *int
  22. collection *string
  23. defaultReplicaPlacement *string
  24. redirectOnRead *bool
  25. disableDirListing *bool
  26. maxMB *int
  27. secretKey *string
  28. dirListingLimit *int
  29. dataCenter *string
  30. enableNotification *bool
  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 example filer.toml configuration file can be generated by "weed scaffold filer"
  62. `,
  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. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  75. Masters: strings.Split(*f.masters, ","),
  76. Collection: *fo.collection,
  77. DefaultReplication: *fo.defaultReplicaPlacement,
  78. RedirectOnRead: *fo.redirectOnRead,
  79. DisableDirListing: *fo.disableDirListing,
  80. MaxMB: *fo.maxMB,
  81. SecretKey: *fo.secretKey,
  82. DirListingLimit: *fo.dirListingLimit,
  83. DataCenter: *fo.dataCenter,
  84. })
  85. if nfs_err != nil {
  86. glog.Fatalf("Filer startup error: %v", nfs_err)
  87. }
  88. if *fo.publicPort != 0 {
  89. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  90. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  91. publicListener, e := util.NewListener(publicListeningAddress, 0)
  92. if e != nil {
  93. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  94. }
  95. go func() {
  96. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  97. glog.Fatalf("Volume server fail to serve public: %v", e)
  98. }
  99. }()
  100. }
  101. glog.V(0).Infoln("Start Seaweed Filer", util.VERSION, "at port", strconv.Itoa(*fo.port))
  102. filerListener, e := util.NewListener(
  103. ":"+strconv.Itoa(*fo.port),
  104. time.Duration(10)*time.Second,
  105. )
  106. if e != nil {
  107. glog.Fatalf("Filer listener error: %v", e)
  108. }
  109. // starting grpc server
  110. grpcPort := *fo.grpcPort
  111. if grpcPort == 0 {
  112. grpcPort = *fo.port + 10000
  113. }
  114. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  115. if err != nil {
  116. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  117. }
  118. grpcS := util.NewGrpcServer()
  119. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  120. reflection.Register(grpcS)
  121. go grpcS.Serve(grpcL)
  122. httpS := &http.Server{Handler: defaultMux}
  123. if err := httpS.Serve(filerListener); err != nil {
  124. glog.Fatalf("Filer Fail to serve: %v", e)
  125. }
  126. }