最近编写了一个给 gin 框架实现注解路由的工具,个人觉得挺有实际意义
位置 github.com/1-st/gin-annotation 实现的效果像这样:
/* 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",
})
}
思路:读取 Go 源代码的 AST 文件,然后生成一个 route.entry.go 路由文件,像这样:
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)
}
}
}
如果觉得有用/有趣,请star,谢谢!
本作品采用《CC 协议》,转载必须注明作者和本文链接