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.

99 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. s3StandaloneOptions 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. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  28. s3StandaloneOptions.filerBucketsPath = cmdS3.Flag.String("filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  29. s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port")
  30. s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}")
  31. s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  32. s3StandaloneOptions.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. return s3StandaloneOptions.startS3Server()
  43. }
  44. func (s3opt *S3Options) startS3Server() bool {
  45. filerGrpcAddress, err := parseFilerGrpcAddress(*s3opt.filer)
  46. if err != nil {
  47. glog.Fatal(err)
  48. return false
  49. }
  50. router := mux.NewRouter().SkipClean(true)
  51. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  52. Filer: *s3opt.filer,
  53. FilerGrpcAddress: filerGrpcAddress,
  54. DomainName: *s3opt.domainName,
  55. BucketsPath: *s3opt.filerBucketsPath,
  56. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  57. })
  58. if s3ApiServer_err != nil {
  59. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  60. }
  61. httpS := &http.Server{Handler: router}
  62. listenAddress := fmt.Sprintf(":%d", *s3opt.port)
  63. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  64. if err != nil {
  65. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  66. }
  67. if *s3opt.tlsPrivateKey != "" {
  68. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3opt.port)
  69. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  70. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  71. }
  72. } else {
  73. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3opt.port)
  74. if err = httpS.Serve(s3ApiListener); err != nil {
  75. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  76. }
  77. }
  78. return true
  79. }