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

Wildcard route conflicts with static files #360

Closed
nazwa opened this issue Jul 2, 2015 · 7 comments · Fixed by #2663
Closed

Wildcard route conflicts with static files #360

nazwa opened this issue Jul 2, 2015 · 7 comments · Fixed by #2663

Comments

@nazwa
Copy link

nazwa commented Jul 2, 2015

Just found an interesting issue:

    r.Static("/assets/", p.rootFolder+"/assets")
    r.GET("/:code", p.redirectHandler)

Generates:

panic: wildcard route ':code' conflicts with existing children in path '/:code'

Seems like it's due to the fact that Static uses *filename wildcard. My plane is due in soon, so can't dig into it now, but I think any static wildcards should be generated below the url path specified, not within root domain.

As in:
/assets/*filename
instead of 
/*filename

Any thoughts?

@manucorporat
Copy link
Contributor

@nazwa the problem is not r.Static("/assets/", p.rootFolder+"/assets"), in fact, it registers /assets/*filename.

The conflict happens because: /assets would match /assets/*filename and /:code.

HttpRouter developer is working in a non-strict mode that will allow this to work.

Workaround:

    r.GET("/c/:code", p.redirectHandler)

@nazwa
Copy link
Author

nazwa commented Jul 3, 2015

I thought placeholders only matched up to the first slash in the path? Are you saying that r.GET("/:code", p.redirectHandler) will match /123 and /123/hello? Thought that's what the wildcard option was for.

@StarpTech
Copy link

StarpTech commented Aug 21, 2017

I also faced the same issue. The root of the issue is the used router https://github.com/julienschmidt That issue is well known and not fixed since a year

julienschmidt/httprouter#210
julienschmidt/httprouter#175
julienschmidt/httprouter#183
...

I don't know why gin use this buggy router implementation there are some great alternatives
https://github.com/avelino/awesome-go#routers

@xaionaro
Copy link

xaionaro commented May 15, 2018

Confirm. My problem:

[GIN-debug] POST   /auth.json                --> github.com/appleboy/gin-jwt.(*GinJWTMiddleware).LoginHandler-fm (3 handlers)
[GIN-debug] POST   /refresh_token.json       --> github.com/appleboy/gin-jwt.(*GinJWTMiddleware).RefreshHandler-fm (3 handlers)
[GIN-debug] POST   /sign_up.json             --> github.com/appleboy/gin-jwt.(*GinJWTMiddleware).LoginHandler-fm (4 handlers)
[GIN-debug] GET    /ping.json                --> github.com/dxcenter/chess/serverMethods.Ping (3 handlers)
[GIN-debug] GET    /whoami.json              --> github.com/dxcenter/chess/serverMethods.Whoami (5 handlers)
[GIN-debug] GET    /games.json               --> github.com/dxcenter/chess/serverMethods.Games (5 handlers)
[GIN-debug] GET    /games/:game_id/status.json --> github.com/dxcenter/chess/serverMethods.GameStatus (5 handlers)
[GIN-debug] POST   /games.json               --> github.com/dxcenter/chess/serverMethods.NewGame (5 handlers)
[GIN-debug] POST   /games/:game_id/move.json --> github.com/dxcenter/chess/serverMethods.Move (5 handlers)
[GIN-debug] GET    /frontend/*filepath       --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /frontend/*filepath       --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] GET    /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] GET    /css/*filepath            --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /css/*filepath            --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] GET    /                         --> github.com/gin-gonic/gin.(*RouterGroup).StaticFile.func1 (3 handlers)
[GIN-debug] HEAD   /                         --> github.com/gin-gonic/gin.(*RouterGroup).StaticFile.func1 (3 handlers)
[GIN-debug] GET    /games/new                --> github.com/gin-gonic/gin.(*RouterGroup).StaticFile.func1 (3 handlers)
panic: path segment 'new' conflicts with existing wildcard ':game_id' in path '/games/new'

I don't see any possibility how /games/new can be in a conflict with /games/:game_id/status.json.

The line:

r.StaticFile("/games/new", "frontend/build/index.html")

@lincolnzhou
Copy link

Is there another solution?

@odiferousmint
Copy link

odiferousmint commented Oct 30, 2019

I have an issue with this as well.

router.Static("/", "./static")
router.GET("/socket.io/", gin.WrapH(server))
router.POST("/socket.io/", gin.WrapH(server))

This works with many other routers but not with Gin! :( I want to serve the directory ./static which contains index.html and js/socket.io.js. How am I supposed to do this?! It makes no sense for it to be in conflict:

/index.html
/js/socket.io.js
/socket.io/

Is there going to be a better way of handling conflicts or is it possible to use a different router? How am I supposed to go about this?

I get this error, by the way:

[GIN-debug] GET    /*filepath                --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /*filepath                --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] GET    /socket.io/               --> github.com/gin-gonic/gin.WrapH.func1 (3 handlers)
panic: '/socket.io/' in new path '/socket.io/' conflicts with existing wildcard '/*filepath' in existing prefix '/*filepath'

There are two workarounds that seem to work:

	router.StaticFile("/", "static/index.html")
	router.StaticFile("/js/socket.io.js", "static/js/socket.io.js")

	router.GET("/socket.io/", gin.WrapH(server))
	router.POST("/socket.io/", gin.WrapH(server))

and:

	router.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "public/")
	})

	router.GET("/socket.io/", gin.WrapH(server))
	router.POST("/socket.io/", gin.WrapH(server))
	router.StaticFS("/public", http.Dir("./static"))

@jseparator
Copy link

jseparator commented Oct 9, 2022

router.NoRoute(gin.WrapH(http.FileServer(http.Dir("static"))))
router.GET("/socket.io/", gin.WrapH(server))
router.POST("/socket.io/", gin.WrapH(server))

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