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.

89 lines
2.8 KiB

7 years ago
7 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "time"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/s3api"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/gorilla/mux"
  10. )
  11. var (
  12. s3options S3Options
  13. )
  14. type S3Options struct {
  15. filer *string
  16. filerGrpcPort *int
  17. filerBucketsPath *string
  18. port *int
  19. domainName *string
  20. tlsPrivateKey *string
  21. tlsCertificate *string
  22. }
  23. func init() {
  24. cmdS3.Run = runS3 // break init cycle
  25. s3options.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  26. s3options.filerGrpcPort = cmdS3.Flag.Int("filer.grpcPort", 0, "filer server grpc port, default to filer http port plus 10000")
  27. s3options.filerBucketsPath = cmdS3.Flag.String("filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  28. s3options.port = cmdS3.Flag.Int("port", 8333, "s3options server http listen port")
  29. s3options.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}")
  30. s3options.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  31. s3options.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file")
  32. }
  33. var cmdS3 = &Command{
  34. UsageLine: "s3 -port=8333 -filer=<ip:port>",
  35. Short: "start a s3 API compatible server that is backed by a filer",
  36. Long: `start a s3 API compatible server that is backed by a filer.
  37. `,
  38. }
  39. func runS3(cmd *Command, args []string) bool {
  40. filerGrpcAddress, err := parseFilerGrpcAddress(*s3options.filer, *s3options.filerGrpcPort)
  41. if err != nil {
  42. glog.Fatal(err)
  43. return false
  44. }
  45. router := mux.NewRouter().SkipClean(true)
  46. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  47. Filer: *s3options.filer,
  48. FilerGrpcAddress: filerGrpcAddress,
  49. DomainName: *s3options.domainName,
  50. BucketsPath: *s3options.filerBucketsPath,
  51. })
  52. if s3ApiServer_err != nil {
  53. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  54. }
  55. httpS := &http.Server{Handler: router}
  56. listenAddress := fmt.Sprintf(":%d", *s3options.port)
  57. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  58. if err != nil {
  59. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  60. }
  61. if *s3options.tlsPrivateKey != "" {
  62. if err = httpS.ServeTLS(s3ApiListener, *s3options.tlsCertificate, *s3options.tlsPrivateKey); err != nil {
  63. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  64. }
  65. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3options.port)
  66. } else {
  67. if err = httpS.Serve(s3ApiListener); err != nil {
  68. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  69. }
  70. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3options.port)
  71. }
  72. return true
  73. }