Home  >  Article  >  Backend Development  >  How to add name to gin route like laravel?

How to add name to gin route like laravel?

WBOY
WBOYforward
2024-02-14 20:51:07671browse

如何像 laravel 一样为 gin 路由添加名称?

php editor Strawberry will introduce you how to add a name to the Gin route like Laravel. Gin is a fast, flexible and efficient Go language web framework, but unlike Laravel, it does not have built-in route name functionality. However, we can achieve this functionality with some simple tricks. In this article, we will share a simple method to add a name to Gin routes to improve the readability and maintainability of the code. let's start!

Question content

I want to add a name to the gin route because I need to collect all routes and display them on the page. route.go

func main() {
    r := gin.Default()
    r.GET("/api/user/list", handler1)
    r.POST("/api/user/create",handle2)  
    r.Run()
}

This is the structure I need to collect:

var Routes []Route
type Route struct {
    Name       string `json:"name"`
    Sign       string `json:"sign"`
    HttpMethod string `json:"http_method"`
    HttpPath   string `json:"http_path"`
}

What have I tried?

1. This is not an automatic method and requires manual maintenance of route names.

var RouteNameMap = map[string]string{
"api.user.list":"Get User List",
"api.user.create": "Create an User"
}

func ParseRoutes(r *gin.Engine, group string) {
    routes := r.Routes()
    for _, route := range routes {
        if strings.HasPrefix(route.Path, group) {
            sign := strings.Replace(strings.Trim(route.Path, "/"), "/", ".", -1)
            item := Route{
                Name:       RouteNameMap[sign],
                Sign:       sign,
                HttpMethod: route.Method,
                HttpPath:   route.Path,
            }
            Routes = append(Routes, item)
        }
    }
}
  1. Use middleware, but this method needs to be obtained after requesting routing. I want to get all the data after the project is started.

3. Similar to the first method:

func main() {
    r := gin.Default()
    MyGET(r,"/api/user/list", handler1,"Get User List")
    MyPOST(r,"/api/user/create",handle2,"Create an User")
    r.Run()         
}

func MyPOST(engine *gin.Engine, path string, handlerFunc gin.HandlerFunc, name string) {
    engine.POST(path, handlerFunc)
    Routes = append(Routes, Route{
        Name:       name,
        Sign:       strings.Replace(strings.Trim(path, "/"), "/", ".", -1),
        HttpMethod: "POST",
        HttpPath:   path,
    })
}
output:
[{
        "name": "Get User List",
        "sign": "api.user.list",
        "http_method": "GET",
        "http_path": "/api/user/list"
    },
    {
        "name": "Create an User",
        "sign": "api.user.create",
        "http_method": "post",
        "http_path": "/api/user/create"
    }
]

But I feel that this method does not meet the standards. I hope to add a similar name parameter to the POST and GET methods of gin. And this name parameter can be parsed in the ParseRoutes method, what should I do? Thank you

Solution

So, what’s the problem with the third method?

If the Route of the Gin framework does not provide parameters for setting Name, then you can only do this:

  1. Submit code to the Gin framework and let the Gin framework support your needs;
  2. Rewrite the Gin framework and abandon subsequent updates of the Gin framework;
  3. Simply encapsulate a function yourself, just like the third method above.

The above is the detailed content of How to add name to gin route like laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete