use the global function of context.ResolveFS on APIBuilder.HandleDir (see prev commit)

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-09-21 23:28:04 +03:00
parent 95a8110f1e
commit fd1db640a0
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503

View File

@ -1,10 +1,8 @@
package router
import (
"embed"
"errors"
"fmt"
stdfs "io/fs"
"net/http"
"os"
"path"
@ -637,33 +635,7 @@ func (api *APIBuilder) HandleDir(requestPath string, fsOrDir interface{}, opts .
options = opts[0]
}
var fs http.FileSystem
switch v := fsOrDir.(type) {
case string:
fs = http.Dir(v)
case http.FileSystem:
fs = v
case embed.FS:
direEtries, err := v.ReadDir(".")
if err != nil {
panic(err)
}
if len(direEtries) == 0 {
panic("HandleDir: no directories found under the embedded file system")
}
subfs, err := stdfs.Sub(v, direEtries[0].Name())
if err != nil {
panic(err)
}
fs = http.FS(subfs)
case stdfs.FS:
fs = http.FS(v)
default:
panic(fmt.Sprintf(`HandleDir: unexpected "fsOrDir" argument type of %T (string or http.FileSystem)`, v))
}
fs := context.ResolveFS(fsOrDir)
h := FileServer(fs, options)
description := "file server"
if d, ok := fs.(http.Dir); ok {