Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcioAlmada/ae46ca47716b5b6f0b5514c51af5e5e0 to your computer and use it in GitHub Desktop.
Save marcioAlmada/ae46ca47716b5b6f0b5514c51af5e5e0 to your computer and use it in GitHub Desktop.
// FileSystem custom file system handler
type FileSystem struct {
fs http.FileSystem
}
// Open opens file
func (fs FileSystem) Open(path string) (http.File, error) {
f, err := fs.fs.Open(path)
if err != nil {
return nil, err
}
s, err := f.Stat()
if s.IsDir() {
index := strings.TrimSuffix(path, "/") + "/index.html"
if _, err := fs.fs.Open(index); err != nil {
return nil, err
}
}
return f, nil
}
fileServer := http.FileServer(FileSystem{http.Dir(directory)})
http.Handle("/", http.StripPrefix(strings.TrimRight(path, "/"), fileServer))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment