2021-03-01 18:04:59 +00:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
|
2021-03-10 09:54:56 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-03-01 18:04:59 +00:00
|
|
|
)
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
func NewWebDAVServer(fs vfs.Filesystem, port int, user, pass string) error {
|
2021-10-11 16:50:18 +00:00
|
|
|
|
|
|
|
srv := newHandler(fs)
|
|
|
|
|
2023-12-21 23:15:39 +00:00
|
|
|
serveMux := http.NewServeMux()
|
|
|
|
|
|
|
|
serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2021-10-11 16:50:18 +00:00
|
|
|
username, password, _ := r.BasicAuth()
|
|
|
|
if username == user && password == pass {
|
|
|
|
srv.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="BASIC WebDAV REALM"`)
|
|
|
|
w.WriteHeader(401)
|
2023-12-21 23:15:39 +00:00
|
|
|
_, _ = w.Write([]byte("401 Unauthorized\n"))
|
2021-10-11 16:50:18 +00:00
|
|
|
})
|
|
|
|
|
2023-12-21 23:15:39 +00:00
|
|
|
//nolint:exhaustruct
|
|
|
|
httpServer := &http.Server{
|
|
|
|
Addr: fmt.Sprintf("0.0.0.0:%d", port),
|
|
|
|
Handler: serveMux,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Str("host", httpServer.Addr).Msg("starting webDAV server")
|
|
|
|
|
|
|
|
return httpServer.ListenAndServe()
|
2021-03-01 18:04:59 +00:00
|
|
|
}
|