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

Root static resource router not supported #2846

Open
LoSunny opened this issue Aug 29, 2021 · 5 comments · May be fixed by #3270
Open

Root static resource router not supported #2846

LoSunny opened this issue Aug 29, 2021 · 5 comments · May be fixed by #3270

Comments

@LoSunny
Copy link

LoSunny commented Aug 29, 2021

Description

This is a follow up of the issue: #2537

How to reproduce

package route

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	var app = gin.Default()

	api.RunRoot(app.Group("/api"))
	app.StaticFS("/", gin.Dir("dist", false))
	app.Run(":8080")
}

Expectations

If the URL starts with the `/api`, then it is handled by the other router, else it searches the file in the `StaticFS`

Actual result

An error occur:
[GIN-debug] GET    /*filepath                --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (4 handlers)
[GIN-debug] HEAD   /*filepath                --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (4 handlers)
[GIN-debug] GET    /api/ping                 --> SunnyMovie/server/route/routes/api.RunRoot.func1 (4 handlers)
panic: '/api/ping' in new path '/api/ping' conflicts with existing wildcard '/*filepath' in existing prefix '/*filepath'

Environment

  • go version: 1.17
  • gin version (or commit ref): v1.7.4
  • operating system: MacOS 10.15.7
@automano
Copy link

automano commented Sep 1, 2021

your code snip is not runnable with the error "undeclared name: api"

@LoSunny
Copy link
Author

LoSunny commented Sep 2, 2021

Sorry, this should work

package route

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	var app = gin.Default()

	app.Group("/api").GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	app.StaticFS("/", gin.Dir("dist", false))
	app.Run(":8080")
}

@automano
Copy link

automano commented Sep 3, 2021

Basically, you can't use gin like this and the panic error message already describes your problem. This problem occurs mainly because many golang frameworks use httprouter as their routing module. httprouter uses explicit matching for performance optimization, which causes many wildcards to conflict with specific routes.

app.StaticFS("/", gin.Dir("dist", false)) will register the wildcard /*filename as the router path but this is already occupied by /api app.Group("/api")

I recommend you don't use the root path to serve your static files.

you may find some other similar issues like #1301

@LoSunny
Copy link
Author

LoSunny commented Sep 5, 2021

But according to #2663 , this issue should be been solved

@automano
Copy link

automano commented Sep 5, 2021

Did a careful read and noticed this "This only works for param wildcards of the form :name and doesn't change the existing * "
Maybe this is why in your situation there is still conflict error.

image

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

Successfully merging a pull request may close this issue.

2 participants