Open Orocker opened 9 months ago
I want to add a name to the gin route because I need to collect all the routes and display them on the page.
maybe help you.
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
var routes gin.RoutesInfo
r.POST("/upload", func(c *gin.Context) {
c.String(http.StatusOK, "OK")
})
r.GET("/user/:id", func(c *gin.Context) {
c.String(http.StatusOK, "OK")
})
r.GET("/routes", func(c *gin.Context) {
type Route struct {
Method string `json:"method"`
Path string `json:"path"`
Handler string `json:"handler"`
}
data := make([]Route, 0, len(routes))
for _, route := range routes {
data = append(data, Route{
Method: route.Method,
Path: route.Path,
Handler: route.Handler,
})
}
c.JSON(200, data)
})
routes = r.Routes()
_ = r.Run(":8080")
}
Description
Question detail:https://stackoverflow.com/questions/77618417/how-to-add-a-name-to-the-gin-route-like-laravel
I want to add a name to the gin route because I need to collect all the routes and display them on the page.
Expectations
Environment