Skip to content

Instantly share code, notes, and snippets.

@gabrielmoura
Created May 19, 2024 01:08
Show Gist options
  • Save gabrielmoura/fd59d722f509e8a13fc873a6c2888023 to your computer and use it in GitHub Desktop.
Save gabrielmoura/fd59d722f509e8a13fc873a6c2888023 to your computer and use it in GitHub Desktop.
A simple WebDav server
package main
import (
"log"
"net/http"
"strings"
"golang.org/x/net/webdav"
)
func main() {
handler := &webdav.Handler{
FileSystem: webdav.Dir(`./test`),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
log.Printf("%s: %s", r.Method, r.URL)
},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// se começar com .well-known
if strings.Split(r.URL.Path, "/")[1] == ".well-known" {
// RFC 5785
if r.URL.Path == "/.well-known/caldav" {
w.Header().Set("Location", "/caldav")
}
if r.URL.Path == "/.well-known/carddav" {
w.Header().Set("Location", "/carddav")
}
}
handler.ServeHTTP(w, r)
})
http.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
http.ListenAndServe(":8080", nil)
}
package main
import (
"encoding/base64"
"log"
"net/http"
"strings"
"golang.org/x/net/webdav"
)
// Hardcoded username and password for the example
const (
username = "user"
password = "password"
)
// basicAuthMiddleware is the middleware function for Basic Authentication
func basicAuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
sendUnauthorized(w)
return
}
authParts := strings.SplitN(auth, " ", 2)
if len(authParts) != 2 || authParts[0] != "Basic" {
sendUnauthorized(w)
return
}
payload, _ := base64.StdEncoding.DecodeString(authParts[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || pair[0] != username || pair[1] != password {
sendUnauthorized(w)
return
}
next.ServeHTTP(w, r)
})
}
// sendUnauthorized sends a 401 Unauthorized response with the Basic authentication challenge
func sendUnauthorized(w http.ResponseWriter) {
w.Header().Set("WWW-Authenticate", `Basic realm="example"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
}
func main() {
handler := &webdav.Handler{
FileSystem: webdav.Dir(`./test`),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
log.Printf("%s: %s", r.Method, r.URL)
},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// se começar com .well-known
if strings.Split(r.URL.Path, "/")[1] == ".well-known" {
// RFC 5785
if r.URL.Path == "/.well-known/caldav" {
w.Header().Set("Location", "/caldav")
}
if r.URL.Path == "/.well-known/carddav" {
w.Header().Set("Location", "/carddav")
}
}
handler.ServeHTTP(w, r)
})
http.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
http.ListenAndServe(":8080", basicAuthMiddleware(http.DefaultServeMux))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment