Skip to content

Instantly share code, notes, and snippets.

@toravir
Created March 16, 2020 05:17
Show Gist options
  • Save toravir/aeb9c2923cd180a71bd15203d1aea4f2 to your computer and use it in GitHub Desktop.
Save toravir/aeb9c2923cd180a71bd15203d1aea4f2 to your computer and use it in GitHub Desktop.
Golang HTTP File Server With DELETE
package main
import (
"net/http"
"fmt"
"os"
)
func main() {
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("/home/ravir/tmp/"))
mux.Handle("/", mw("/home/ravir/tmp/", fs))
http.ListenAndServe("localhost:11223", mux)
}
func mw (dirprefix string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" {
file2Del := dirprefix+r.URL.String()
fmt.Println("Deleting file:", file2Del)
os.Remove(file2Del)
return
}
next.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment