Beerus 1.1.7 来啦,带来了 5 大特性

2021-12-26 17:30:37 +08:00
 Joker123456789

本次更新的点如下

用形参接收请求参数

只需要将定义好的 struct 加入到路由函数的参数列表即可

route.GET("/test", func(param DemoParam) {

})

type DemoParam struct {
    Name string `field:"name"`
    Age int `field:"age"`
    Friends []string
}

前端请求这个路由的时候,参数会被自动提取到 param 中

在 JSON 模式和非 JSON 模式之间切换

默认就是 JSON 模式,JSON 模式有以下特性

一个返回值的示例

返回类型可以是 struct ,map ,数组,这里为了演示方便就用的 map

route.GET("/test", func(param DemoParam, req commons.BeeRequest, res commons.BeeResponse) map[string]string{

    msg := make(map[string]string)
    msg["msg"] = "hello word"
  
    // 直接返回即可,beerus 会自动将他转成 json 响应给前端
    return msg
})

两个返回值的示例

第一个返回值的类型同上,第二个返回值必须是 error 类型

route.GET("/test", func(param DemoParam, req commons.BeeRequest, res commons.BeeResponse) (map[string]string, error) {
  
    if xxx {
      return nil, errors.New("错误提示信息")
    }
  
    msg := make(map[string]string)
    msg["msg"] = "hello word"
  
    // 直接返回即可,beerus 会自动将他转成 json 响应给前端
    return msg, nil
})

关闭 JSON 模式,很简单,只需要在创建路由前 执行以下代码

route.JsonMode = false

JSON 模式一旦关闭,那么 JSON 模式的特性将会全部失效,路由函数不可以有返回值,我们需要手动调用 Validation 函数实现参数验证,手动调用 res.SendXXX 函数实现数据响应

route.GET("/test", func(param DemoParam, req commons.BeeRequest, res commons.BeeResponse) {
    
    var result = params.Validation(req, &param)
    
    // 当返回的不是 SUCCESS ,就说明验证没通过
    if result != params.SUCCESS {
        // 这里可以自己选择合适的 SendXXX 函数
        res.SendErrorMsg(1128, result)
        return
    }
    
    // 这里可以自己选择合适的 SendXXX 函数
    res.SendJson(`{"msg":"hello word"}`)
    
})

错误处理机制

看了上面的 JSON 模式,大家应该猜到了,错误处理机制就是在路由函数上设置第二个返回值,把我们需要响应给前端的错误提示信息 通过第二个返回值返回即可

route.GET("/test", func(param DemoParam, req commons.BeeRequest, res commons.BeeResponse) (map[string]string, error) {
  
    if xxx {
      return nil, errors.New("错误提示信息")
    }
  
    msg := make(map[string]string)
    msg["msg"] = "hello word"
  
    // 直接返回即可,beerus 会自动将他转成 json 响应给前端
    return msg, nil
})

拦截器更好的兼容 JSON 和非 JSON 模式

之前的拦截器是,如果放行 就返回 SUCCESS 常量,否则返回提示信息;这种做法有一个缺陷,就是只能以 JSON 的形式 给前端发送提示,如果开发者不想用 JSON 跟前端交互,那么这种设计将会是一个缺陷。

现在改成了,拦截器返回一个 bool ,如果放行就返回 true ,否则返回 false ,但是有一个注意点:如果你要返回 false ,那么就必须先调用 res.SendXXX 函数给前端一个响应,不然本次请求会被阻塞到超时,对性能造成影响*

func CreateInterceptor() {
  route.AddInterceptor("/example/*", loginInterceptorBefore)
}

func loginInterceptorBefore(req *commons.BeeRequest, res *commons.BeeResponse) bool {
  res.
  SetHeader("hello", "hello word").
  SetHeader("hello2", "word2")

  log.Println("exec interceptor")
  return true
}

ToStruct 和 Validation 函数 简化了一下

以前调用 ToStruct 需要这样,传入指针 和值,有一点麻烦

param := DemoParam{}
params.ToStruct(req, &param, param)

现在改成了这样,只需要传指针

param := DemoParam{}
params.ToStruct(req, &param)

虽然,有了形参接收参数的功能以后,这种写法可能会被渐渐淘汰掉,但是依然会有被使用的可能性,所以暂时保留了下来,并做了一个优化。


以前调用 Validation 需要这样,传入指针和值,有一点麻烦

var result = params.Validation(req, &param, param)
if result != params.SUCCESS {

  // Non-json mode also can not be returned in this way, you need to call the Send function in the res object to return the result to the front end
  res.SendErrorMsg(500, result)
  return
}

现在改成了这样,只需要传指针

var result = params.Validation(req, &param)
if result != params.SUCCESS {

  // Non-json mode also can not be returned in this way, you need to call the Send function in the res object to return the result to the front end
  res.SendErrorMsg(500, result)
  return
}

感兴趣的伙伴们可以访问官网查看更多哦

https://beeruscc.com

1672 次点击
所在节点    Go 编程语言
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/824513

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX