Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Questions and fixes about filesystem middleware #1546

Closed
fufuok opened this issue Sep 28, 2021 · 1 comment · Fixed by #1547
Closed

🐛 Questions and fixes about filesystem middleware #1546

fufuok opened this issue Sep 28, 2021 · 1 comment · Fixed by #1547

Comments

@fufuok
Copy link
Contributor

fufuok commented Sep 28, 2021

Fiber version

v2.19.0

Issue description

didn't make it today unfortunately, try to provide a fix on the weekend, if you have time before and find a solution, then feel free to create a pull request

Originally posted by @ReneWerner87 in #1439 (comment)

Hi, Great fiber.

A few days ago, I raised the bug and PR of app.Static. #1538

After looking at the long-standing problems above, I tested some bugs.

Code snippet

package main

import (
	"embed"
	"log"
	"net/http"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/filesystem"
)

// Embed a single file
//go:embed index.html
var f embed.FS

// Embed a directory
//go:embed static/*
var embedDirStatic embed.FS

//go:embed index/*
var embedDirIndex embed.FS

func main() {
	app := fiber.New()

	app.Use("/", filesystem.New(filesystem.Config{
		Root: http.FS(f),
	}))

	// Access file "image.png" under `static/` directory via URL: `http://<server>/static/image.png`.
	// Without `PathPrefix`, you have to access it via URL:
	// `http://<server>/static/static/image.png`.
	app.Use("/static", filesystem.New(filesystem.Config{
		Root:       http.FS(embedDirStatic),
		PathPrefix: "static",
		Browse:     true,
	}))

	app.Use("/static_dir_home", filesystem.New(filesystem.Config{
		Root: http.FS(f),
	}))

	app.Use("/index_dir_home", filesystem.New(filesystem.Config{
		Root:       http.FS(embedDirIndex),
		PathPrefix: "index",
	}))

	log.Fatal(app.Listen(":3000"))
}

Directory

.
├── bug-images
│   ├── 1.png
│   ├── 2.png
│   └── 3.png
├── go.mod
├── go.sum
├── index
│   ├── file.txt
│   └── index.html
├── index.html
├── static
│   ├── dir
│   │   ├── file_dir.txt
│   │   └── subdir
│   │       └── file.txt
│   └── file_static.txt
└── tmp_filesystem_embed.go

Bug

image

image

image

image

test code

With or without embed and test code after repair.

test_code_filesystem_embed.zip

@fufuok
Copy link
Contributor Author

fufuok commented Sep 28, 2021

Run *_fixed in the code package above to get the correct result below.

tmp_filesystem_fixed:

1.

image

tmp_filesystem_embed_fixed:

1.

image

2.

image

3.

image

go.mod

module tmp_filesystem_embed_fixed

go 1.16

require github.com/gofiber/fiber/v2 v2.19.0

replace github.com/gofiber/fiber/v2 v2.19.0 => github.com/fufuok/fiber/v2 v2.19.1-0.20210928153748-195aa2a3a79b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants