webdav.go 3.1 KB

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