这是一个更加极简版本的go框架,由基础的net/http封装而成,大部分参考了gin。
相比于New创建新的引擎,更推荐使用Default来创建,如果您有更好的日志或者错误恢复方案,也可忽略。
r := GoMatrix.New()
// or
r := GoMatrix.Default()同时,框架支持SSL证书,需要使用SslNew来进行,只需将证书位置写在其中即可:
r := GoMatrix.SslNew("./gomatrix.crt","./gomatrix.key")启动,只需要调用Run方法即可,您需要传入您的ip、端口号以及期待的最大连接数量,此数值没有默认,可根据情况动态变化。
err := r.Run("localhost","8080",10000)
if err != nil {
return
}如此,便开启了一个http服务
GoMatrix提供了GET、POST、PUT、DELETE、PATCH、CONNECT、OPTIONS、TRACE、HEAD等HTTP Request
例:
r := GoMatrix.Default()
r.GET("/", func(c *GoMatrix.Context) {
c.JSON(http.StatusOK, "hello world")
})
err := r.Run("localhost","8080",10000)
if err != nil {
return
}在路由定义时,定义路径参数需要以:或者*开头,请注意,以*开头,则认为其后的全都是参数变量,获取其参数使用Param方法,例如:
r.GET("/:param", func(c *GoMatrix.Context) {
s := c.Param("param")
c.String(http.StatusOK,s)
})使用Query方法获取
r.GET("/", func(c *GoMatrix.Context) {
s := c.Query("param")
c.String(http.StatusOK,s)
})使用PostForm方法获取
r.POST("/", func(c *GoMatrix.Context) {
s := c.PostForm("param")
c.String(http.StatusOK,s)
})
GoMatrix提供了诸多Response达成需要的响应,例如application/json
r.GET("/", func(c *GoMatrix.Context) {
c.JSON(http.StatusOK, "hello world")
})亦或text/plain
r.GET("/", func(c *GoMatrix.Context) {
c.String(http.StatusOK, "hello world")
})亦或者文件
r.GET("/", func(c *GoMatrix.Context) {
c.Data(http.StatusOK,[]byte("hello world"))
})当然,文件亦可下载
r.GET("/", func(c *GoMatrix.Context) {
c.Download(Path, fileName)
})使用方法:
r := GoMatrix.Default()
r.GET("/", func(c *GoMatrix.Context) {
s := c.Query("param")
c.String(http.StatusOK,s)
})
api := r.Group("/api")
api.GET("/", func(c *GoMatrix.Context) {
s := c.Query("param")
c.String(http.StatusOK,s)
})引擎和分组都可用于创建API