webdav-go/main.go

51 lines
955 B
Go
Raw Normal View History

2022-01-10 16:31:57 +00:00
package main
import (
"log"
"net/http"
"os"
"golang.org/x/net/webdav"
)
const (
envPrefix = "WEBDAV_PREFIX"
pathRoot = "/webdav/root"
2022-01-10 16:56:41 +00:00
pathLog = "/dev/stdout"
2022-01-10 16:31:57 +00:00
)
func logger() func(*http.Request, error) {
return func(r *http.Request, err error) {
if err != nil {
log.Printf("ERROR %v\n", err)
}
}
}
func filesystem() webdav.FileSystem {
if err := os.Mkdir(pathRoot, os.ModePerm); !os.IsExist(err) {
log.Fatalf("FATAL %v", err)
}
log.Printf("INFO using local filesystem at %s", pathRoot)
return webdav.Dir(pathRoot)
}
func main() {
logFile, err := os.OpenFile(pathLog, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("FATAL %v", err)
}
defer logFile.Close()
log.SetOutput(logFile)
h := &webdav.Handler{
Prefix: os.Getenv(envPrefix),
FileSystem: filesystem(),
LockSystem: webdav.NewMemLS(),
Logger: logger(),
}
http.HandleFunc("/", h.ServeHTTP)
http.ListenAndServe(":8080", h)
}