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

栏目: 服务器 · 发布时间: 5年前

内容简介:go-mir v1.0.0 发布了,推荐使用。go-mir 是一个使用 golang 结构体标签信息将方法注册为 http engine handler 的辅助库,目前支持将方法注册到

go-mir v1.0.0 发布了,推荐使用。

新增特性

  • mir: 添加对handler的直接赋值

  • mir: 支持对单独field条目添加chain function信息,支持对单一路由handler插入middleware信息,目前基于go-chi的module/chi子模块支持该功能

  • module/chi: 添加对 go-chi 路由框架的支持

使用go-mir构建web服务的样例代码

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

主要功能:

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

  • 通过反射机制根据结构体标签信息获取结构体方法,并依据结构体标签信息注册到相应的 http engine 中,比如 GinEchoIrisMacaronMuxhttprouter , 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()
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据之美

数据之美

邱南森 (Nathan Yau) / 张伸 / 中国人民大学出版社 / 2014-2-1 / CNY 89.00

这是一本教我们如何制作完美可视化图表,挖掘大数据背后意义的书。作者认为,可视化是一种媒介,向我们揭示了数据背后的故事。他循序渐进、深入浅出地道出了数据可视化的步骤和思想。本书让我们知道了如何理解数据可视化,如何探索数据的模式和寻找数据间的关联,如何选择适合自己的数据和目的的可视化方式,有哪些我们可以利用的可视化工具以及这些工具各有怎样的利弊。 作者给我们提供了丰富的可视化信息以及查看、探索数......一起来看看 《数据之美》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

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

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具