1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

add support for embed.FS

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-19 01:15:38 +03:00
parent 71e8a14615
commit 414c1ad1ae
15 changed files with 179 additions and 22 deletions

View File

@@ -1,8 +1,10 @@
package router
import (
"embed"
"errors"
"fmt"
stdfs "io/fs"
"net/http"
"os"
"path"
@@ -633,6 +635,21 @@ func (api *APIBuilder) HandleDir(requestPath string, fsOrDir interface{}, opts .
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)
default:
panic(fmt.Errorf(`unexpected "fsOrDir" argument type of %T (string or http.FileSystem)`, v))
}