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.

93 lines
2.9 KiB

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