Beerusgo 开发的 web 框架

联合创作 · 2023-09-20 01:24

Beerus 是 一个用 go 开发的 web 框架,属于 Beerus 的子项目之一,底层基于 go 自带的 net/http,在此基础上扩展了很多功能,比如:

  • 路由管理
  • 会话管理
  • 拦截器
  • 实体接参数
  • 参数自动化验证
  • WebSocket

文档

https://beeruscc.com/beerus

示例

HTTP 示例

创建一个函数管理路由

func CreateRoute() {
	
    // 打开JSON模式,默认就是开启的,这里为了演示 所以手工开了一下,实际是不需要的
    route.JsonMode = true
    
    // 任意请求方式,都可以像这样 使用形参接收请求参数
    // 路由函数必须有返回值,支持struct,map,数组,这里为了演示方便,直接用的map
    route.GET("/example/get", func (param DemoParam) map[string]string{
		
        // 直接返回即可,beerus会自动转成json  响应给前端
        msg := make(map[string]string)
        msg["msg"] = "success"
        return msg
    })
}

// 接收参数的实体
type DemoParam struct {
    TestStringReception  string  `notnull:"true" msg:"TestStringReception Cannot be empty" routes:"/example/put"`
    TestIntReception     int     `max:"123" min:"32" msg:"TestIntReception The value range must be between 32 - 123" routes:"/example/post"`
    TestUintReception    uint    `max:"123" min:"32" msg:"TestUintReception The value range must be between 32 - 123"`
    TestFloatReception   float32 `max:"123" min:"32" msg:"TestFloatReception The value range must be between 32 - 123"`
    TestBoolReception    bool
    TestStringRegReception string `reg:"^[a-z]+$" msg:"TestStringRegReception Does not meet the regular"`
    TestBeeFileReception commons.BeeFile
    
    TestJsonReception []string
}

启动服务

func main() {
    // Interceptors, routes, etc. Loading of data requires its own calls
    routes.CreateRoute()
    
    // Listen the service and listen to port 8080
    beerus.ListenHTTP(8080)
}

非JSON模式

func CreateRoute() {

    // 关闭JSON模式,默认是开启的,想要关闭 就必须手工设置为false
    route.JsonMode = false
    
        
    // 一样支持形参接收参数,但是路由函数不可以有返回值
    route.POST("/example/post", func (param  DemoParam, req commons.BeeRequest, res commons.BeeResponse) {
        
        // 非JSON模式下,必须手工调用函数 完成参数验证
        var result = params.Validation(req, &param, param)
        if result != params.SUCCESS {
      
            // 可以响应任意类型的数据,这里为了演示方便 就还是用的json
            res.SendErrorMsg(1128, result)
            return
        }
        
        // 可以响应任意类型的数据,这里为了演示方便 就还是用的json
        res.SendJson(`{"msg":"SUCCESS"}`)
    })
}

// 接收参数的实体
type DemoParam struct {
    TestStringReception  string  `notnull:"true" msg:"TestStringReception Cannot be empty" routes:"/example/put"`
    TestIntReception     int     `max:"123" min:"32" msg:"TestIntReception The value range must be between 32 - 123" routes:"/example/post"`
    TestUintReception    uint    `max:"123" min:"32" msg:"TestUintReception The value range must be between 32 - 123"`
    TestFloatReception   float32 `max:"123" min:"32" msg:"TestFloatReception The value range must be between 32 - 123"`
    TestBoolReception    bool
    TestStringRegReception string `reg:"^[a-z]+$" msg:"TestStringRegReception Does not meet the regular"`
    TestBeeFileReception commons.BeeFile
    
    TestJsonReception []string
}

WebSocket 示例

创建一个函数来管理WebSocket路由

func CreateWebSocketRoute() {
	wroute.AddWebSocketRoute("/ws/test", onConnection, onMessage, onClose)
	wroute.AddWebSocketRoute("/ws/test2", onConnection, onMessage, onClose)
}

// In order to save time, only three functions are used below. In practice, you can configure a set of functions for each wroute

func onConnection(session *wparams.WebSocketSession, msg string) {
	session.SendString("connection success")
}

func onMessage(session *wparams.WebSocketSession, msg string) {
	session.SendString("I got the message.")
}

func onClose(session *wparams.WebSocketSession, msg string) {
    println(msg + "-------------------------------")
}

启动服务

func main() {
    // Interceptors, routes, etc. Loading of data requires its own calls
    routes.CreateRoute()
    routes.CreateWebSocketRoute()
    
    // Listen the service and listen to port 8080
    beerus.ListenHTTP(8080)
}

想了解更多的话,可以查阅我们的文档哦

浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报