Recently, I wrote a tool to implement annotation routing for gin framework, which I think is of great practical significance

Time:2021-9-21

positiongithub.com/1-st/gin-annotationThe effect is like this:


/* Hello a simple controller
[
    method:GET,
    groups:/api,
    path:/hello-world,
    need:auth
]
*/
func HelloWorld(ctx *gin.Context) {
    ctx.JSON( http.StatusOK, map[string]string{
        "msg": "hello, world",
    })
}

Idea: read the ast file of go source code, and then generate a route.entry.go route file, like this:

import (
    "gin-annotation/_example/simple/controller"
    "gin-annotation/_example/simple/middleware"
    "github.com/gin-gonic/gin"
)

func Route(e *gin.Engine) {
    api := e.Group("/api", middleware.Log)
    {
        v1 := api.Group("/v1")
        {
            v1.GET("/hello-world", middleware.Auth, controller.HelloWorld)
        }
    }
}

If you find it useful / interesting, please star, thank you!


go

This work adoptsCC agreement, reprint must indicate the author and the link to this article