xujiajun/gorouter

static file handler is too complex

Closed this issue · 1 comments

user need to add url partern for every depth folder ?
is there a short way to match a total folder as below

// 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(".")})
http.Handle("/static/", http.StripPrefix(strings.TrimRight("/static/", "/"), fileServer))

You can use regular expressions to define the rules you require.