Description
I created a new app and installed the latest gin 1.9.1. My simple main file looks like this:
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "OK",
})
})
r.Run(":8003")
}
With gin@v.1.9.1
GET request to any route return 404. When I switch to gin@v1.9.0
everything works fine.
Interestingly, when I move the route "/"
up, before the "/ping"
it magically works again even with gin@v.1.9.1
. It is like the first route is dynamic and ties all other routes dynamically.
Also when I add new routes below if the API path continues like this "/ping", "/ping1", "/ping12", "/ping123" they all work. But when the route path is "/something-else" it doesn't work anymore if it is after "/ping" route.
How to reproduce
Described in the description since it is really easily reproduced.
Expectations
The routes should work for both versions.
Actual result
curl --location 'http://localhost:8003/ping'
returns 404
for gin@v.1.9.1
Environment
- go version: go1.20rc3
- gin version (or commit ref):
- operating system: Mac OS
Comment From: bmf-san
Umm, I thought it was interesting and tried it, but it didn't reproduce.
It looks like there is no difference related to the router. https://github.com/gin-gonic/gin/compare/v1.9.0...v1.9.1
I'm sorry if the execution environment is different. 😢
Environment
go version:go1.20.3 darwin/arm64 os: Mac OS
Codes
codes used for reproduce
main.gopackage main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "OK",
})
})
r.Run(":8003")
}
go.mod
module example.com
go 1.20
require github.com/gin-gonic/gin v1.9.1
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Logs
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] GET / --> main.main.func2 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8003
[GIN] 2023/07/22 - 22:14:18 | 200 | 47.667µs | 127.0.0.1 | GET "/"
[GIN] 2023/07/22 - 22:14:21 | 200 | 151.875µs | 127.0.0.1 | GET "/ping"
Comment From: rishiraj88
Have we got any clues by now?
Comment From: ikeboy003
Have we got any clues by now?
I tried to replicate as well and it worked for both versions using go version go1.20.5 darwin/arm64. Wish i could replicate :/
Comment From: iayamowner
I had the same problem:
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /metrics --> github.com/gin-gonic/gin.WrapH.func1 (5 handlers)
[GIN-debug] GET /swagger/*any --> github.com/swaggo/gin-swagger.CustomWrapHandler.func1 (5 handlers)
[GIN-debug] GET /api-docs --> api/api.Run.func1 (5 handlers)
[GIN-debug] GET /api-docs.json --> api/api.Run.func2 (5 handlers)
[GIN-debug] POST /api/v1/ --> api/api.handleAction (6 handlers)
[GIN] 2023/08/22 - 12:54:51 | 404 | 252.458µs | 127.0.0.1 | POST "/api/v1/demo/get"
Comment From: tinkrtailor
Does it work in the browser? I found that I am able to make requests using the browser but not via curl