webdav.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os/user"
  6. "strconv"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/server"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/spf13/viper"
  13. )
  14. var (
  15. webDavStandaloneOptions WebDavOption
  16. )
  17. type WebDavOption struct {
  18. filer *string
  19. port *int
  20. collection *string
  21. tlsPrivateKey *string
  22. tlsCertificate *string
  23. }
  24. func init() {
  25. cmdWebDav.Run = runWebDav // break init cycle
  26. webDavStandaloneOptions.filer = cmdWebDav.Flag.String("filer", "localhost:8888", "filer server address")
  27. webDavStandaloneOptions.port = cmdWebDav.Flag.Int("port", 7333, "webdav server http listen port")
  28. webDavStandaloneOptions.collection = cmdWebDav.Flag.String("collection", "", "collection to create the files")
  29. webDavStandaloneOptions.tlsPrivateKey = cmdWebDav.Flag.String("key.file", "", "path to the TLS private key file")
  30. webDavStandaloneOptions.tlsCertificate = cmdWebDav.Flag.String("cert.file", "", "path to the TLS certificate file")
  31. }
  32. var cmdWebDav = &Command{
  33. UsageLine: "webdav -port=7333 -filer=<ip:port>",
  34. Short: "<unstable> start a webdav server that is backed by a filer",
  35. Long: `start a webdav server that is backed by a filer.
  36. `,
  37. }
  38. func runWebDav(cmd *Command, args []string) bool {
  39. util.LoadConfiguration("security", false)
  40. glog.V(0).Infof("Starting Seaweed WebDav Server %s at https port %d", util.VERSION, *webDavStandaloneOptions.port)
  41. return webDavStandaloneOptions.startWebDav()
  42. }
  43. func (wo *WebDavOption) startWebDav() bool {
  44. filerGrpcAddress, err := parseFilerGrpcAddress(*wo.filer)
  45. if err != nil {
  46. glog.Fatal(err)
  47. return false
  48. }
  49. // detect current user
  50. uid, gid := uint32(0), uint32(0)
  51. if u, err := user.Current(); err == nil {
  52. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  53. uid = uint32(parsedId)
  54. }
  55. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  56. gid = uint32(parsedId)
  57. }
  58. }
  59. ws, webdavServer_err := weed_server.NewWebDavServer(&weed_server.WebDavOption{
  60. Filer: *wo.filer,
  61. FilerGrpcAddress: filerGrpcAddress,
  62. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  63. Collection: *wo.collection,
  64. Uid: uid,
  65. Gid: gid,
  66. })
  67. if webdavServer_err != nil {
  68. glog.Fatalf("WebDav Server startup error: %v", webdavServer_err)
  69. }
  70. httpS := &http.Server{Handler: ws.Handler}
  71. listenAddress := fmt.Sprintf(":%d", *wo.port)
  72. webDavListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  73. if err != nil {
  74. glog.Fatalf("WebDav Server listener on %s error: %v", listenAddress, err)
  75. }
  76. if *wo.tlsPrivateKey != "" {
  77. glog.V(0).Infof("Start Seaweed WebDav Server %s at https port %d", util.VERSION, *wo.port)
  78. if err = httpS.ServeTLS(webDavListener, *wo.tlsCertificate, *wo.tlsPrivateKey); err != nil {
  79. glog.Fatalf("WebDav Server Fail to serve: %v", err)
  80. }
  81. } else {
  82. glog.V(0).Infof("Start Seaweed WebDav Server %s at http port %d", util.VERSION, *wo.port)
  83. if err = httpS.Serve(webDavListener); err != nil {
  84. glog.Fatalf("WebDav Server Fail to serve: %v", err)
  85. }
  86. }
  87. return true
  88. }