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.

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