用 go 结构体标签定义 handler 路由信息的辅助库 go-mir

码农软件 · 软件分类 · 其他开发相关 · 2019-10-15 22:13:51

软件介绍

go-mir 是一个使用 golang 结构体标签信息将方法注册为 http engine handler 的辅助库,目前支持将方法注册到 GinEchoIrisMacaronMuxhttprouter

主要功能:

  • 使用 go 结构体标签定义 handler 的路由信息用于注册

  • 通过反射机制根据结构体标签信息获取结构体方法,并依据结构体标签信息注册到相应的 http engine 中,比如 GinEchoIrisMacaronMuxhttprouter

  • 使用结构体方法编写 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()
}

本文地址:https://codercto.com/soft/d/16845.html

财富博客

财富博客

Robert Scoble、Shel Israel / 李宛蓉 / 重庆出版社 / 2008 / 38.00元

《财富博客》作者斯考伯(美国最多人阅读的企业博客作者)和谢尔•以色列(资深技术顾问)相信,博客已经开始改变企业的面貌。他们阐释说明了员工博客如何改变外界对微软的观感,敢说敢做的NBA球队老板如何运用博客和球迷连接,小企业和大公司又如何能从博客获益……另外,还有万一使用不当,博客又会招致怎样的灾难。斯考伯和以色列直言不讳,这是卓越博客的基本要件,因此他们在说完博客的好处之后,也以同样诚实的心态讨论博......一起来看看 《财富博客》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具