如何在 Gin 中添加具有相同路径前缀和参数的不同端点?
How to add different endpoints with the same path prefix and param in Gin?
我使用 Gin
框架在 Go
中构建了一个 API。我在 API 中有一条路线,我可以通过 id
获得用户。但现在我也想通过用户名获取用户。所以我尝试了与 id
获取用户相同的方法。但它在编译期间给我一个错误。你们能告诉我该怎么做吗?
谢谢。
路线 -
grp.GET("/users", controllers.GetUsers)
grp.GET("/users/:id", controllers.GetUser)
grp.GET("/users/:username", controllers.GetUserByUsername) //error - panic: ':username' in new path '/users/:username' conflicts with existing wildcard ':id' in existing prefix '/users/:id'
grp.POST("/users", controllers.CreateUser)
grp.PATCH("/users/:id", controllers.UpdateUser)
grp.DELETE("/users/:id", controllers.DeleteUser)
控制器-
func GetUser(c *gin.Context) {
paramID := c.Params.ByName("id")
ctx := context.Background()
sa := option.WithCredentialsFile(firestoreFilePath)
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
dsnap, err := client.Collection("users").Doc(paramID).Get(ctx)
if err != nil {
fmt.Print(err)
c.IndentedJSON(http.StatusNotFound, gin.H{
"message": "User not found",
})
return
}
m := dsnap.Data()
c.IndentedJSON(http.StatusNotFound, gin.H{
"User": m,
"message": "User returned successfully",
})
}
API 响应 -
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"email": "Nathan@yesenia.net",
}
]
这是杜松子酒中的一个已知限制。您必须使所有路径都是唯一的。喜欢添加前缀如下:
grp.GET("/users/username/:username", controllers.GetUserByUsername)
有关此问题线程的更多信息:
https://github.com/gin-gonic/gin/issues/1301
我使用 Gin
框架在 Go
中构建了一个 API。我在 API 中有一条路线,我可以通过 id
获得用户。但现在我也想通过用户名获取用户。所以我尝试了与 id
获取用户相同的方法。但它在编译期间给我一个错误。你们能告诉我该怎么做吗?
谢谢。
路线 -
grp.GET("/users", controllers.GetUsers)
grp.GET("/users/:id", controllers.GetUser)
grp.GET("/users/:username", controllers.GetUserByUsername) //error - panic: ':username' in new path '/users/:username' conflicts with existing wildcard ':id' in existing prefix '/users/:id'
grp.POST("/users", controllers.CreateUser)
grp.PATCH("/users/:id", controllers.UpdateUser)
grp.DELETE("/users/:id", controllers.DeleteUser)
控制器-
func GetUser(c *gin.Context) {
paramID := c.Params.ByName("id")
ctx := context.Background()
sa := option.WithCredentialsFile(firestoreFilePath)
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
dsnap, err := client.Collection("users").Doc(paramID).Get(ctx)
if err != nil {
fmt.Print(err)
c.IndentedJSON(http.StatusNotFound, gin.H{
"message": "User not found",
})
return
}
m := dsnap.Data()
c.IndentedJSON(http.StatusNotFound, gin.H{
"User": m,
"message": "User returned successfully",
})
}
API 响应 -
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"email": "Nathan@yesenia.net",
}
]
这是杜松子酒中的一个已知限制。您必须使所有路径都是唯一的。喜欢添加前缀如下:
grp.GET("/users/username/:username", controllers.GetUserByUsername)
有关此问题线程的更多信息: https://github.com/gin-gonic/gin/issues/1301