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

🐛 [Bug]: Static server in sub app does not work #3104

Open
3 tasks done
lephattan opened this issue Aug 19, 2024 · 5 comments
Open
3 tasks done

🐛 [Bug]: Static server in sub app does not work #3104

lephattan opened this issue Aug 19, 2024 · 5 comments

Comments

@lephattan
Copy link

Question Description

I am trying to set app a static server in a sub app, but it does not seem to work.
The problem can be reproduced with the snippet below.

Project struture:

tree .
#.
#├── go.mod
#├── go.sum
#├── main.go
#└── public
#    └── test.txt

Tests:

cat public/test.txt
# test

curl http://127.0.0.1:3000/
# Hello, world!%

curl http://127.0.0.1:3000/public/test.txt
# test

curl http://127.0.0.1:3000/subApp
# Hello from subApp%

curl http://127.0.0.1:3000/subApp/public/test.txt
# Cannot GET /subApp/public/test.txt%

Code Snippet (optional)

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/logger"
)

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

	app.Static("/public", "./public")

	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello, world!")
	})

	subApp := fiber.New()
	subApp.Use(logger)

	subApp.Static("/public", "./public")

	subApp.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello from subApp")
	})

	app.Mount("/subApp", subApp)
	app.Listen(":3000")
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Aug 19, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

thx for sharing, which version do you use?

@lephattan
Copy link
Author

@ReneWerner87 I am using fiber v2.52.5 with go 1.23.0

thx for sharing, which version do you use?

@Skyenought
Copy link
Member

fiber/router.go

Line 404 in 6e74114

fileHandler(c.fasthttp)

@ReneWerner87
I found that when using mount, although the expected path of fs.root is correct, the result of fasthttp ctx.Path is '/' and '/public', which causes the access path to change from public to public/public, which is the reason for this bug.

fs := &fasthttp.FS{
	Root:                 root,

https://github.com/valyala/fasthttp/blob/c3050516d957b5bf75b353683c50302e4f955c6b/fs.go#L1074

// expect: ctx.Path ="/" filepath = "E:\projects_go\server\public"
// fact: ctx.Path = "/public" filepath = "E:\projects_go\server\public\public" 
ff, err = h.openFSFile(filePath, mustCompress, fileEncoding)
@gaby gaby changed the title 🤗 [Question]: Static server in sub app does not work Sep 9, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 14, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 14, 2024
@yinheli
Copy link
Contributor

yinheli commented Sep 14, 2024

Same issue for me. So I submit a PR.

yinheli added a commit to yinheli/fiber that referenced this issue Sep 14, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 14, 2024
@gaby gaby added this to the v3 milestone Sep 14, 2024
@gaby gaby added the v2 label Sep 15, 2024
@gaby gaby removed this from the v3 milestone Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
yinheli added a commit to yinheli/fiber that referenced this issue Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment