Created
April 3, 2018 14:53
-
-
Save Integralist/faffc17e27185cd8271e942f69a811b2 to your computer and use it in GitHub Desktop.
[Golang Prevent Directory Listing with Static FileServer] #static #fileserver #go #golang
This file contains hidden or 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
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