您现在的位置是:首页 > 文章详情

go-mir v2.3.2 发布,用 Go 结构体标签定义 handler 路由信息的辅助库

日期:2020-04-01点击:393

go-mir v2.3.2 发布了,支持多个web框架,自带mirc脚手架,零基础开发web应用,方便快捷。

新增特性:

  • 添加echo, iris, macaron的支持,mirc自动生成相应web框架样式的接口代码;
  • 支持多层次Group定义,优化生成代码的包名命名方式;
  • 支持使用多goroutine更快速的自动 生成接口代码;

简要介绍:

go-mir v2 是参考grpc使用方式,使用golang内置的结构体标签定义路由信息,通过代码生成的方式生成接口代码,业务逻辑只要实现了接口,注册接口实现的对象到相应的web引擎,启动后就可以对外通过RESTful接口获取服务。大致框架如下:

功能特性:

  • 使用Go结构体标签定义handler路由信息;
  • 自动根据定义的结构体标签信息生成handler接口,开发者实现相应接口后注册到router,与gRPC的使用方式类似;
  • 内置支持gingo-chimuxhttprouterechoirismacaron的代码生成器;
  • 自带脚手架mirc自动生成gingo-chimuxhttprouterechoirismacaron样式的模板工程代码;
  • 支持多goroutine并发生成接口代码,加快代码生成效率;

代码示例:(eg: gin style)

  • 生成样板代码
 % go get github.com/alimy/mir/mirc/v2@latest % mirc new -d mir-examples % tree mir-examples mir-examples ├── Makefile ├── README.md ├── go.mod ├── main.go └── mirc ├── main.go └── routes ├── site.go ├── v1 │   └── site.go └── v2 └── site.go % cd mir-examples % make generate
  • 自定义路由信息,比如:
 // file: mirc/routes/v1/site.go package v1 import ( "github.com/alimy/mir/v2" "github.com/alimy/mir/v2/engine" ) func init() { engine.AddEntry(new(Site)) } // Site mir's struct tag define type Site struct { Chain mir.Chain `mir:"-"` Group mir.Group `mir:"v1"` Index mir.Get `mir:"/index/"` Articles mir.Get `mir:"/articles/:category/"` }
  • 定义生成器入口,比如:
 // file: mirc/main.go package main import ( "log" "github.com/alimy/mir/v2/core" "github.com/alimy/mir/v2/engine" _ "github.com/alimy/mir/v2/examples/mirc/routes" _ "github.com/alimy/mir/v2/examples/mirc/routes/v1" _ "github.com/alimy/mir/v2/examples/mirc/routes/v2" ) //go:generate go run main.go func main() { log.Println("generate code start") opts := core.Options{ core.RunMode(core.InSerialDebugMode), core.GeneratorName(core.GeneratorGin), core.SinkPath("./gen"), } if err := engine.Generate(opts); err != nil { log.Fatal(err) } log.Println("generate code finish") }
  • 自动生成接口,基于上面的定义,生成器将自动生成接口定义文件,如下:
 % make generate % cat mirc/gen/api/v1/site.go // Code generated by go-mir. DO NOT EDIT. package v1 import ( "github.com/gin-gonic/gin" ) type Site interface { // Chain provide handlers chain for gin Chain() gin.HandlersChain Index(*gin.Context) Articles(*gin.Context) } // RegisterSiteServant register Site servant to gin func RegisterSiteServant(e *gin.Engine, s Site) { router := e.Group("v1") // use chain for router middlewares := s.Chain() router.Use(middlewares...) // register routes info to router router.Handle("GET", "/index/", s.Index) router.Handle("GET", "/articles/:category/", s.Articles) }
  • 实现接口逻辑, 比如:
 // file: servants/site_v1.go package servants import ( "net/http" "github.com/gin-gonic/gin" api "github.com/alimy/mir/v2/examples/mirc/gen/api/v1" ) var _ api.Site = EmptySiteV1{} // EmptySiteV1 implement api.Site interface type EmptySiteV1 struct{} func (EmptySiteV1) Chain() gin.HandlersChain { return gin.HandlersChain{gin.Logger()} } func (EmptySiteV1) Index(c *gin.Context) { c.String(http.StatusOK, "get index data (v1)") } func (EmptySiteV1) Articles(c *gin.Context) { c.String(http.StatusOK, "get articles data (v1)") }
  • 注册接口实现对象到相对应的router,比如:
 package main import ( "log" "github.com/gin-gonic/gin" "github.com/alimy/mir/v2/examples/mirc/gen/api" "github.com/alimy/mir/v2/examples/mirc/gen/api/v1" "github.com/alimy/mir/v2/examples/mirc/gen/api/v2" "github.com/alimy/mir/v2/examples/servants" ) func main() { e := gin.New() // register servants to engine registerServants(e) // start servant service if err := e.Run(); err != nil { log.Fatal(err) } } func registerServants(e *gin.Engine) { // register default group routes api.RegisterSiteServant(e, servants.EmptySiteWithNoGroup{}) // register routes for group v1 v1.RegisterSiteServant(e, servants.EmptySiteV1{}) // register routes for group v2 v2.RegisterSiteServant(e, servants.EmptySiteV2{}) }
  • 最后,构建并运行应用:
 % make run
  • 大功告成,是不是很简单,赶紧上手吧:)

演示项目:

examples:一个简单的快速了解如何使用mir的演示项目;

mir-covid19:  新冠疫情统计TH_COVID19_International的Golang版本,完整的演示了如何使用mir快速开发一个web应用。

原文链接:https://www.oschina.net/news/114545/go-mir-2-3-2-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章