go-mir v1.0.2 發布了,推薦使用。
前段時間忙於公司業務,go-mir斷更了好長時間,現在有閑余時間,正在籌劃著go-mir的升級。
go-mir v1的架構大體是這樣:
這套架構主要是使用了golang的反射機制對struct tag解析然後註冊路由信息到web engine,隻影響啟動時間,不會有運行時損耗,總體來說,方便了介面定義,對代碼組織很有益處。
go-mir v2版本正在籌劃中,已經開始敲代碼了,大體架構如下:
v2版本將升級採用代碼生成的方式生成介面代碼,同樣也是採用golang內置的struct tag定義路由信息,但不同於v1版本在引擎啟動時解析后註冊路由信息到web引擎,這裡參考grpc的介面生成方式,生成介面定義文件,業務邏輯只要實現了介面,註冊介面實現的對象到相應的web引擎,啟動后就可以對外通過RESTfull介面獲取服務。
go-mir v3版本將會直接使用OpenAPI v3定義介面,大體架構如下:
v3版本將使用OpenApi v3.0的定義文件直接生成介面代碼,後面的邏輯和v2保持一致。使用OpenAPI v3.0定義RESTfull API介面非常清晰、方便的,一直都想從OpenApi 的定義文件直接生成golang介面文件,go-mir v3版本將提供這個特性的支持,敬請期待。
github.com/alimy/mir-music 這是一個使用go-mir + gin 構建的簡單web服務樣例代碼,是spring-music的go實現
github.com/alimy/chi-music 這是使用go-mir + go-chi 構建的簡單web服務樣例代碼, 來源於spring-music的go實現
go-mir 是一個使用 golang 結構體標籤信息將方法註冊為 http engine handler 的輔助庫,目前支持將方法註冊到 Gin, Echo, Iris, Macaron, Mux, httprouter, go-chi。
主要功能:
使用 go 結構體標籤定義 handler 的路由信息用於註冊
通過反射機制根據結構體標籤信息獲取結構體方法,並依據結構體標籤信息註冊到相應的 http engine 中,比如 Gin, Echo, Iris, Macaron, Mux, httprouter, go-chi
使用結構體方法編寫 http handler
使用結構體標籤定義 Middleware 信息,並註冊到 http engine 中
代碼示例:(eg: gin backend)
Get Mir.Gin module first
go get github.com/alimy/mir/module/gin@master
Then happy in codding enjoy your heart...
package main import( "github.com/alimy/mir" "github.com/gin-gonic/gin" "net/http" mirE "github.com/alimy/mir/module/gin" ) type site struct { Chain mir.Chain `mir:"-"` Group mir.Group `mir:"v1"` index mir.Get `mir:"/index/"` articles mir.Get `mir:"/articles/:category/#GetArticles"` } // Index handler of the index field that in site struct, the struct tag indicate // this handler will register to path "/index/" and method is http.MethodGet. func (h *site) Index(c *gin.Context) { c.String(http.StatusOK, "get index data") } // GetArticles handler of articles indicator that contains Host/Path/Queries/Handler info. // Path info is the second or first(if no host info) segment start with '/'(eg: /articles/:category/#GetArticles) // Handler info is forth info start with '#' that indicate real handler method name(eg: GetArticles).if no handler info will // use field name capital first char as default handler name(eg: if articles had no #GetArticles then the handler name will // is Articles) func (h *site) GetArticles(c *gin.Context) { c.String(http.StatusOK, "get articles data") } func main() { //Create a new gin engine engine := gin.New() // Register handler to engine by mir mirE.Register(engine, &site{Chain: gin.HandlersChain{gin.Logger()}}) // Start gin engine serve engine.Run() }
[admin
]