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-03-10 09:54:56 +00:00
|
|
|
log.Info().Str("host", fmt.Sprintf("0.0.0.0:%d", port)).Msg("starting webDAV server")
|
2021-10-11 16:50:18 +00:00
|
|
|
|
|
|
|
srv := newHandler(fs)
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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)
|
|
|
|
w.Write([]byte("401 Unauthorized\n"))
|
|
|
|
})
|
|
|
|
|
|
|
|
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
|
2021-03-01 18:04:59 +00:00
|
|
|
}
|