Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Integralist/faffc17e27185cd8271e942f69a811b2 to your computer and use it in GitHub Desktop.
Save Integralist/faffc17e27185cd8271e942f69a811b2 to your computer and use it in GitHub Desktop.
[Golang Prevent Directory Listing with Static FileServer] #static #fileserver #go #golang
func noDirListing(h http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)
return
}
h.ServeHTTP(w, r)
})
}
func main() {
http.Handle("/", noDirListing(http.FileServer(http.Dir("./static/"))))
log.Println(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment