Disable directory listing with http.FileServer

9,723 views
Skip to first unread message

Trung Pham

unread,
Oct 9, 2012, 3:50:07 AM10/9/12
to golan...@googlegroups.com
Hi,

I'm using the built in static file http.FileServer.
Do you know if there is an easy way to disable the directory listing? Just simply return a 404.

Thanks.

minux

unread,
Oct 9, 2012, 10:50:12 AM10/9/12
to Trung Pham, golan...@googlegroups.com
On Tue, Oct 9, 2012 at 3:50 PM, Trung Pham <tr...@phamcom.com> wrote:
I'm using the built in static file http.FileServer.
Do you know if there is an easy way to disable the directory listing? Just simply return a 404.
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))
}

Brad Fitzpatrick

unread,
Oct 9, 2012, 10:52:46 AM10/9/12
to Trung Pham, golan...@googlegroups.com
package main

import (
"net/http"
"os"
)

type justFilesFilesystem struct {
fs http.FileSystem
}

func (fs justFilesFilesystem) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
return neuteredReaddirFile{f}, nil
}

type neuteredReaddirFile struct {
http.File
}

func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}

func main() {
fs := justFilesFilesystem{http.Dir("/tmp/")}
http.ListenAndServe(":8080", http.FileServer(fs))
}


--
 
 

Trung Pham

unread,
Oct 10, 2012, 12:10:55 AM10/10/12
to golan...@googlegroups.com, Trung Pham
Thanks. It worked perfectly.

From looking the fileServer source code, it always redirects /assets to /assets/. I hope that this functionality will not go away, else it will break this code. :)

Henrik Johansson

unread,
Oct 10, 2012, 2:33:10 AM10/10/12
to Brad Fitzpatrick, Trung Pham, golan...@googlegroups.com
The simplicity of this tiny bit of code is such a big part of what I
like about Go that I would probably settle for this alone.
It has all the concepts of the entire type system all in one little
functional snippet. Well damn near at least.

Pretty!
> --
>
>

ilkergok...@gmail.com

unread,
Feb 9, 2015, 2:57:37 PM2/9/15
to golan...@googlegroups.com
A more cleaner approach would be like this:

publicPath = "./public"

router.Methods("GET").PathPrefix("/").HandlerFunc(publicHandler)

func publicHandler(w http.ResponseWriter, r *http.Request) {
path := publicPath + "/" + r.URL.Path
if f, err := os.Stat(path); err == nil && !f.IsDir() {
http.ServeFile(w, r, path)
return
}

http.NotFound(w, r)
}

Ingo Oeser

unread,
Feb 10, 2015, 4:20:18 AM2/10/15
to golan...@googlegroups.com
Brad, would you mind adding that as an example to net/http? Most of the asset helper libraries out there solve mostly this problem for users in a bloated way.

arm...@gmail.com

unread,
Mar 16, 2016, 1:08:22 AM3/16/16
to golang-nuts, tr...@phamcom.com
I've settled on the following, which has the added benefit of returning 404 for directories.

Corrections appreciated.


type justFilesFilesystem struct {
Fs http.FileSystem

}

func (fs justFilesFilesystem) Open(name string) (http.File, error) {
   f, err := fs.Fs.Open(name)

if err != nil {
return nil, err
}

   stat, err := f.Stat()
if stat.IsDir() {
return nil, os.ErrNotExist
}

return f, nil
}

ko...@ekanna.com

unread,
Jun 24, 2017, 10:31:33 PM6/24/17
to golang-nuts, tr...@phamcom.com
It won't serve index.html file by default like in case of neuteredReaddirFile
Reply all
Reply to author
Forward
0 new messages