Skip to content

Instantly share code, notes, and snippets.

@staaldraad
Last active June 18, 2024 14:34
Show Gist options
  • Save staaldraad/d835126cd46969330a8fdadba62b9b69 to your computer and use it in GitHub Desktop.
Save staaldraad/d835126cd46969330a8fdadba62b9b69 to your computer and use it in GitHub Desktop.
A small webdav server in go
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"golang.org/x/net/webdav"
)
var dir string
func main() {
dirFlag := flag.String("d", "./", "Directory to serve from. Default is CWD")
httpPort := flag.Int("p", 80, "Port to serve on (Plain HTTP)")
httpsPort := flag.Int("ps", 443, "Port to serve TLS on")
serveSecure := flag.Bool("s", false, "Serve HTTPS. Default false")
flag.Parse()
dir = *dirFlag
srv := &webdav.Handler{
FileSystem: webdav.Dir(dir),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Printf("WEBDAV [%s]: %s, ERROR: %s\n", r.Method, r.URL, err)
} else {
log.Printf("WEBDAV [%s]: %s \n", r.Method, r.URL)
}
},
}
http.Handle("/", srv)
if *serveSecure == true {
if _, err := os.Stat("./cert.pem"); err != nil {
fmt.Println("[x] No cert.pem in current directory. Please provide a valid cert")
return
}
if _, er := os.Stat("./key.pem"); er != nil {
fmt.Println("[x] No key.pem in current directory. Please provide a valid cert")
return
}
go http.ListenAndServeTLS(fmt.Sprintf(":%d", *httpsPort), "cert.pem", "key.pem", nil)
}
if err := http.ListenAndServe(fmt.Sprintf(":%d", *httpPort), nil); err != nil {
log.Fatalf("Error with WebDAV server: %v", err)
}
}
@athornton
Copy link

Thank you!

@athornton
Copy link

if you're interested, https://github.com/lsst-sqre/worblehat is where I ended up. It still needs GH actions configuration and stuff, but the Go code seems to be working quite well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment