bokofs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package bokofs
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "sync"
  9. "time"
  10. "golang.org/x/net/webdav"
  11. "imuslab.com/bokofs/bokofsd/mod/bokofs/bokoworker"
  12. )
  13. /*
  14. WebDAV module
  15. This module handle the interfacing to the underlying file system
  16. through the middle ware
  17. */
  18. type Options struct {
  19. ListeningAddress string //Listening address, e.g. 0.0.0.0:8443
  20. SecureServe bool //Use TLS to serve the WebDAV request
  21. PublicKeyPath string
  22. PrivateKeyPath string
  23. }
  24. type Server struct {
  25. LoadedWorkers sync.Map //Storing uuid to bokoworker pointer (*bokoworker.Worker)
  26. RootRouter FlowRouter
  27. Options *Options
  28. }
  29. /* NewWebdavInterfaceServer creates a new WebDAV server instance */
  30. func NewWebdavInterfaceServer(options Options) (*Server, error) {
  31. thisServer := Server{
  32. LoadedWorkers: sync.Map{},
  33. Options: &options,
  34. }
  35. //TODO: Load all the middlewares
  36. //Initiate the root router file system
  37. err := thisServer.InitiateRootRouter()
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &thisServer, nil
  42. }
  43. func (s *Server) AddWorker(worker *bokoworker.Worker) {
  44. s.LoadedWorkers.Store(worker.RootPath, worker)
  45. }
  46. func (s *Server) RemoveWorker(workerRootPath string) {
  47. s.LoadedWorkers.Delete(workerRootPath)
  48. }
  49. func (s *Server) Start() error {
  50. srv := &webdav.Handler{
  51. FileSystem: s.RootRouter,
  52. LockSystem: webdav.NewMemLS(),
  53. Logger: func(r *http.Request, err error) {
  54. if err != nil {
  55. log.Printf("WEBDAV [%s]: %s, ERROR: %s\n", r.Method, r.URL, err)
  56. } else {
  57. log.Printf("WEBDAV [%s]: %s \n", r.Method, r.URL)
  58. }
  59. },
  60. }
  61. http.Handle("/", srv)
  62. if s.Options.SecureServe {
  63. if _, err := os.Stat(s.Options.PublicKeyPath); err != nil {
  64. fmt.Println("Public ket not found")
  65. return err
  66. }
  67. if _, err := os.Stat(s.Options.PrivateKeyPath); err != nil {
  68. fmt.Println("Private key not found")
  69. return err
  70. }
  71. }
  72. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  73. defer cancel()
  74. done := make(chan error)
  75. go func() {
  76. if s.Options.SecureServe {
  77. if err := http.ListenAndServeTLS(s.Options.ListeningAddress, s.Options.PublicKeyPath, s.Options.PrivateKeyPath, nil); err != nil {
  78. done <- err
  79. }
  80. } else {
  81. if err := http.ListenAndServe(s.Options.ListeningAddress, nil); err != nil {
  82. log.Fatalf("Error with WebDAV server: %v", err)
  83. done <- err
  84. }
  85. }
  86. }()
  87. select {
  88. case <-ctx.Done():
  89. //No error in 3 seconds. Assume all green
  90. return nil
  91. case success := <-done:
  92. if success != nil {
  93. log.Fatalf("Error with WebDAV server")
  94. return success
  95. }
  96. }
  97. return nil
  98. }