package main

import (
	"fmt"
	"net/http"
	"path/filepath"
)

func main() {
	fileServer := http.FileServer(neuteredFs{http.Dir("./static")})

	http.Handle("/", fileServer)
	err := http.ListenAndServe(":8000", nil)

	if err != nil {
		fmt.Println(err)
	}
}


func (nfs neuteredFs) Open(path string) (http.File, error) {
	f, err := nfs.fs.Open(path)
    if err != nil {
        return nil, err
    }

    s, err := f.Stat()
    if err != nil {
        return nil, err
    }

    if s.IsDir() {
        index := filepath.Join(path, "index.html")
        if _, err := nfs.fs.Open(index); err != nil {
            closeErr := f.Close()
            if closeErr != nil {
                return nil, closeErr
            }

            return nil, err
        }
    }

    return f, nil
}

type neuteredFs struct {
	fs http.FileSystem
}
