Created
March 16, 2020 05:17
-
-
Save toravir/aeb9c2923cd180a71bd15203d1aea4f2 to your computer and use it in GitHub Desktop.
Golang HTTP File Server With DELETE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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