Open LoSunny opened 3 years ago
your code snip is not runnable with the error "undeclared name: api"
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")
}
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
But according to https://github.com/gin-gonic/gin/pull/2663 , this issue should be been solved
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.
Description
This is a follow up of the issue: https://github.com/gin-gonic/gin/issues/2537
How to reproduce
Expectations
Actual result
Environment
1.17
v1.7.4
MacOS 10.15.7