Skip to content
This repository has been archived by the owner on Mar 31, 2020. It is now read-only.

Commit

Permalink
Add file server which disables directory listing (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
soedar authored Dec 21, 2017
1 parent 7c6d426 commit 3707690
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions web/fileserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package web

import (
"net/http"
"os"
)

// FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root.
// This implementation will always returns 404 Not Found if the request is a directory, and will not serve `index.html`.
// https://groups.google.com/d/msg/golang-nuts/bStLPdIVM6w/wSKqNoaSji8J
func FileServer(root http.Dir) http.Handler {
return http.FileServer(justFilesFilesystem{root})
}

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
}

0 comments on commit 3707690

Please sign in to comment.