回顾
上一期,我们讲述了一个简单的web应用的搭建和访问。这一期我们的主要内容是
gin中间件的使用 post和get请求的参数获取方式 ...
中间件
使用场景:
auth权限认证 跨域 其他需要在业务处理之前处理的事务
我们接着上一期的工程继续
查看一下目录
➜ go-gin-test tree -L 3
.
├── go.mod
├── go.sum
├── hello
├── mian.go
└── routerex
└── router.go
routerexmidmidmid.go
在请求到达服务后,但是还未进行业务处理时,需要验证请求是否合法
mid.go
package mid
import (
"github.com/gin-gonic/gin"
)
func midAuth(c *gin.Context) {
//获取请求的ip地址
ip := c.ClientIP()
//如果请求地址来源不正确,那么阻止这个请求继续
if ip != "baidu.com" {
println("ip 地址不正确")c.Abort()
return
}
// 处理请求
c.Next()
}
router.go
package routerex
import (
"example.com/m/v2/routerex/mid"
"github.com/gin-gonic/gin"
)
func InitRouter(g *gin.Engine) {
g1 := g.Group("g1")
//中间件
g1.Use(mid.MidAuth)
g1.GET("/hello1", func(c *gin.Context) {
c.JSON(200, gin.H{
"msg": "Hello g1",
})
})
g2 := g.Group("g2")
g2.GET("/hello2", func(c *gin.Context) {
c.JSON(200, gin.H{
"msg": "Hello g2",
})
})
}
go build -o hello
➜ go-gin-test go build -o hello
➜ go-gin-test ls
go.mod go.sum hello mian.go
./hello
http://localhost:8080/g1/hello1

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /g1/hello1 --> example.com/m/v2/routerex.InitRouter.func1 (4 handlers)
[GIN-debug] GET /g2/hello2 --> example.com/m/v2/routerex.InitRouter.func2 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
ip 地址不正确
[GIN] 2021/04/06 - 17:00:38 | 200 | 61.658µs | ::1 | GET "/g1/hello1"
可以看到我们的浏览器上没有任何输出
ip 地址不正确
通过这个思路,我们可以实现很多业务或者场景的需求。
跨域中间件
func midCors(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Headers", "*")
c.Header("Access-Control-Allow-Methods", "*")
c.Header("Access-Control-Allow-Credentials", "true")
//放行所有OPTIONS方法
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
// 处理请求
c.Next()
}
POST和GET获取值的方式
Parameters in path (url路径参数)
name := c.Param("name")
Querystring parameters
//这个写法具有默认值
firstname := c.DefaultQuery("firstname", "Guest")
Multipart/Urlencoded Form(表单)
//无默认值
message := c.PostForm("message")
//有默认值
nick := c.DefaultPostForm("nick", "anonymous")
Map as querystring or postform parameters(Map或者表单)
ids := c.QueryMap("ids")
names := c.PostFormMap("names")
file(文件类型)
form, _ := c.MultipartForm()
files := form.File["upload[]"]
JSON
//结构体
j := J{}
c.BindJSON(j)
当然除了上述常用的方式之外,gin还提供的更加丰富的获取参数的方式。
大家可以通过上面的demo来尝试一下。
下一期的内容预告
如何使用gin构建一个标准的web工程 如何使用gin写出标准的api接口 ...
超级英雄吉姆gin
超级英雄吉姆