Skip to content

Instantly share code, notes, and snippets.

@staaldraad
Last active April 14, 2024 03:53
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • 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

What license is this under? Specifically, is it OK to adapt it for an MIT-licensed project of mine?

@staaldraad
Copy link
Author

Hey @athornton ; absolutely fine, there is no license attached but MIT would also make sense. Feel free to use it how ever you please, no attribution needed

@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