tstor/src/export/webdav/http.go

39 lines
829 B
Go
Raw Normal View History

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"
"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 {
srv := newHandler(fs)
2023-12-21 23:15:39 +00:00
serveMux := http.NewServeMux()
serveMux.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)
2023-12-21 23:15:39 +00:00
_, _ = w.Write([]byte("401 Unauthorized\n"))
})
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
}