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

When I run the Basic Routing code, there are some routing matching problems🤗 #1090

Closed
thep0y opened this issue Dec 26, 2020 · 3 comments
Closed
Assignees

Comments

@thep0y
Copy link

thep0y commented Dec 26, 2020

Question description

The responses of route /:name and route /:name/:age/:gender? are correct.
The responses of route /:file.:ext and route /flights/:from-:to and route /api/* are wrong:

Hello, dictionary.txt 👋!
👴 flights is LAX-SFO years old
👴 api is register years old

Which code is incorrect?

Code snippet Optional

package main

import (
	"fmt"
	"log"

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

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

	// GET /john
	app.Get("/:name", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
		return c.SendString(msg) // => Hello john 👋!
	})

	// GET /john/75
	app.Get("/:name/:age/:gender?", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("👴 %s is %s years old", c.Params("name"), c.Params("age"))
		return c.SendString(msg) // => 👴 john is 75 years old
	})

	// GET /dictionary.txt
	app.Get("/:file.:ext", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("📃 %s.%s", c.Params("file"), c.Params("ext"))
		return c.SendString(msg) // => 📃 dictionary.txt
	})

	// GET /flights/LAX-SFO
	app.Get("/flights/:from-:to", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("💸 From: %s, To: %s", c.Params("from"), c.Params("to"))
		return c.SendString(msg) // => 💸 From: LAX, To: SFO
	})

	// GET /api/register
	app.Get("/api/*", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("✋ %s", c.Params("*"))
		return c.SendString(msg) // => ✋ register
	})

	log.Fatal(app.Listen(":3000"))
}
@welcome
Copy link

welcome bot commented Dec 26, 2020

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

thanks for the report, the challenge with these routes is that they use many variable parts on the same levels, here it is important to consider the order

at fiber we get the order of the routes specified by the user to exclude possible side effects or unforeseen processes

since we have chosen this order, the router will be faster by simply processing each route one after the other, we could also sort these routes so that routes with static elements come first, but this will be quite difficult with variable parts, as these require perfect prioritization by a human, as here quite complex possibilities are possible

in real microservice cases also mostly a prefix is used, so this sorting is not important there, because not several variable parts are on the same base, like here in the root path

here is the cause described again #1088

with this order of the route, everything is working

the fixed routes first, then those which are less variable and then the really variable ones

package main

import (
	"fmt"
	"log"

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

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

	// GET /api/register
	app.Get("/api/*", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("✋ %s", c.Params("*"))
		return c.SendString(msg) // => ✋ register
	})

	// GET /flights/LAX-SFO
	app.Get("/flights/:from-:to", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("💸 From: %s, To: %s", c.Params("from"), c.Params("to"))
		return c.SendString(msg) // => 💸 From: LAX, To: SFO
	})

	// GET /dictionary.txt
	app.Get("/:file.:ext", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("📃 %s.%s", c.Params("file"), c.Params("ext"))
		return c.SendString(msg) // => 📃 dictionary.txt
	})

	// GET /john/75
	app.Get("/:name/:age/:gender?", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("👴 %s is %s years old", c.Params("name"), c.Params("age"))
		return c.SendString(msg) // => 👴 john is 75 years old
	})

	// GET /john
	app.Get("/:name", func(c *fiber.Ctx) error {
		msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
		return c.SendString(msg) // => Hello john 👋!
	})



	log.Fatal(app.Listen(":3000"))
}
@ReneWerner87
Copy link
Member

@thep0y any comments or questions ? otherwise i would close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants