1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package bokofs
- import (
- "log"
- "net/http"
- "os"
- "sync"
- "golang.org/x/net/webdav"
- "imuslab.com/bokofs/bokofsd/mod/bokofs/bokoworker"
- )
- /*
- WebDAV module
- This module handle the interfacing to the underlying file system
- through the middle ware
- */
- type Server struct {
- LoadedWorkers sync.Map //Storing uuid to bokoworker pointer (*bokoworker.Worker)
- RootRouter FlowRouter
- prefix string
- }
- /* NewWebdavInterfaceServer creates a new WebDAV server instance */
- func NewWebdavInterfaceServer(pathPrfix string) (*Server, error) {
- thisServer := Server{
- LoadedWorkers: sync.Map{},
- prefix: pathPrfix,
- }
- //Initiate the root router file system
- err := thisServer.InitiateRootRouter()
- if err != nil {
- return nil, err
- }
- return &thisServer, nil
- }
- func (s *Server) AddWorker(worker *bokoworker.Worker) error {
- //Check if the worker root path is already loaded
- if _, ok := s.LoadedWorkers.Load(worker.RootPath); ok {
- return os.ErrExist
- }
- s.LoadedWorkers.Store(worker.RootPath, worker)
- return nil
- }
- func (s *Server) RemoveWorker(workerRootPath string) {
- s.LoadedWorkers.Delete(workerRootPath)
- }
- func (s *Server) Handler() http.Handler {
- srv := &webdav.Handler{
- FileSystem: s.RootRouter,
- LockSystem: webdav.NewMemLS(),
- Logger: func(r *http.Request, err error) {
- if err != nil {
- log.Printf("WEBDAV [%s]: %s, ERROR: %s\n", r.Method, r.URL, err)
- } else {
- log.Printf("WEBDAV [%s]: %s \n", r.Method, r.URL)
- }
- },
- }
- return srv
- }
|