diff --git a/middleware/filesystem/README.md b/middleware/filesystem/README.md index 6785506f3b..ddf1b4c883 100644 --- a/middleware/filesystem/README.md +++ b/middleware/filesystem/README.md @@ -92,11 +92,11 @@ func main() { })) // Access file "image.png" under `static/` directory via URL: `http:///static/image.png`. - // With `http.FS(embedDirStatic)`, you have to access it via URL: + // Without `PathPrefix`, you have to access it via URL: // `http:///static/static/image.png`. - subFS, _ := fs.Sub(embedDirStatic, "static") app.Use("/static", filesystem.New(filesystem.Config{ - Root: http.FS(subFS), + Root: http.FS(embedDirStatic), + PathPrefix: "static" Browse: true, })) @@ -251,6 +251,14 @@ type Config struct { // Required. Default: nil Root http.FileSystem `json:"-"` + // PathPrefix defines a prefix to be added to a filepath when + // reading a file from the FileSystem. + // + // Use when using Go 1.16 embed.FS + // + // Optional. Default "" + PathPrefix string `json:"path_prefix"` + // Enable directory browsing. // // Optional. Default: false @@ -278,10 +286,11 @@ type Config struct { ```go var ConfigDefault = Config{ - Next: nil, - Root: nil, - Browse: false, - Index: "/index.html", - MaxAge: 0, + Next: nil, + Root: nil, + PathPrefix: "", + Browse: false, + Index: "/index.html", + MaxAge: 0, } ``` diff --git a/middleware/filesystem/filesystem.go b/middleware/filesystem/filesystem.go index a05adbff27..88eb79958b 100644 --- a/middleware/filesystem/filesystem.go +++ b/middleware/filesystem/filesystem.go @@ -23,6 +23,14 @@ type Config struct { // Required. Default: nil Root http.FileSystem `json:"-"` + // PathPrefix defines a prefix to be added to a filepath when + // reading a file from the FileSystem. + // + // Use when using Go 1.16 embed.FS + // + // Optional. Default "" + PathPrefix string `json:"path_prefix"` + // Enable directory browsing. // // Optional. Default: false @@ -47,11 +55,12 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ - Next: nil, - Root: nil, - Browse: false, - Index: "/index.html", - MaxAge: 0, + Next: nil, + Root: nil, + PathPrefix: "", + Browse: false, + Index: "/index.html", + MaxAge: 0, } // New creates a new middleware handler @@ -79,6 +88,10 @@ func New(config ...Config) fiber.Handler { panic("filesystem: Root cannot be nil") } + if cfg.PathPrefix != "" && !strings.HasPrefix(cfg.PathPrefix, "/") { + cfg.PathPrefix = "/" + cfg.PathPrefix + } + var once sync.Once var prefix string var cacheControlStr = "public, max-age=" + strconv.Itoa(cfg.MaxAge) @@ -107,6 +120,11 @@ func New(config ...Config) fiber.Handler { if !strings.HasPrefix(path, "/") { path = "/" + path } + // Add PathPrefix + if cfg.PathPrefix != "" { + // PathPrefix already has a "/" prefix + path = cfg.PathPrefix + path + } var ( file http.File diff --git a/middleware/filesystem/filesystem_test.go b/middleware/filesystem/filesystem_test.go index 4a69c4dbd8..fb71d40329 100644 --- a/middleware/filesystem/filesystem_test.go +++ b/middleware/filesystem/filesystem_test.go @@ -32,6 +32,11 @@ func Test_FileSystem(t *testing.T) { NotFoundFile: "index.html", })) + app.Use("/prefix", New(Config{ + Root: http.Dir("../../.github/testdata/fs"), + PathPrefix: "img", + })) + tests := []struct { name string url string @@ -96,6 +101,12 @@ func Test_FileSystem(t *testing.T) { statusCode: 200, contentType: "text/html", }, + { + name: "PathPrefix should be applied", + url: "/prefix/fiber.png", + statusCode: 200, + contentType: "image/png", + }, } for _, tt := range tests {