tstor/src/mounts/webdav/http.go
royalcat d30ef6cc9c
Some checks failed
mkdocs / mkdocs (push) Failing after 2s
CodeQL / Analyze (go) (push) Failing after 2s
CodeQL / Analyze (javascript) (push) Failing after 2s
docker / build-docker (inux/arm/v6) (push) Failing after 14s
docker / build-docker (linux/386) (push) Failing after 13s
docker / build-docker (linux/amd64) (push) Has started running
docker / build-docker (linux/arm/v7) (push) Failing after 14s
docker / build-docker (linux/arm64) (push) Has been cancelled
rework 2 (working)
2023-10-16 12:18:40 +03:00

29 lines
710 B
Go

package webdav
import (
"fmt"
"net/http"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
"github.com/rs/zerolog/log"
)
func NewWebDAVServer(fs vfs.Filesystem, port int, user, pass string) error {
log.Info().Str("host", fmt.Sprintf("0.0.0.0:%d", port)).Msg("starting webDAV server")
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)
}