Beerus go 开发的 web 框架开源项目

我要开发同款
匿名用户2021年12月14日
19阅读
开发技术GO语言
所属分类Google Go、Web框架、Web应用开发
授权协议MIT

作品详情

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

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

https://beeruscc.com/beerus

示例HTTP示例创建一个函数管理路由

funcCreateRoute(){//打开JSON模式,默认就是开启的,这里为了演示所以手工开了一下,实际是不需要的route.JsonMode=true//任意请求方式,都可以像这样使用形参接收请求参数//路由函数必须有返回值,支持struct,map,数组,这里为了演示方便,直接用的maproute.GET("/example/get",func(paramDemoParam)map[string]string{//直接返回即可,beerus会自动转成json响应给前端msg:=make(map[string]string)msg["msg"]="success"returnmsg})}//接收参数的实体typeDemoParamstruct{TestStringReceptionstring`notnull:"true"msg:"TestStringReceptionCannotbeempty"routes:"/example/put"`TestIntReceptionint`max:"123"min:"32"msg:"TestIntReceptionThevaluerangemustbebetween32-123"routes:"/example/post"`TestUintReceptionuint`max:"123"min:"32"msg:"TestUintReceptionThevaluerangemustbebetween32-123"`TestFloatReceptionfloat32`max:"123"min:"32"msg:"TestFloatReceptionThevaluerangemustbebetween32-123"`TestBoolReceptionboolTestStringRegReceptionstring`reg:"^[a-z]+$"msg:"TestStringRegReceptionDoesnotmeettheregular"`TestBeeFileReceptioncommons.BeeFileTestJsonReception[]string}

启动服务

funcmain(){//Interceptors,routes,etc.Loadingofdatarequiresitsowncallsroutes.CreateRoute()//Listentheserviceandlistentoport8080beerus.ListenHTTP(8080)}

非JSON模式

funcCreateRoute(){//关闭JSON模式,默认是开启的,想要关闭就必须手工设置为falseroute.JsonMode=false//一样支持形参接收参数,但是路由函数不可以有返回值route.POST("/example/post",func(paramDemoParam,reqcommons.BeeRequest,rescommons.BeeResponse){//非JSON模式下,必须手工调用函数完成参数验证varresult=params.Validation(req,&param,param)ifresult!=params.SUCCESS{//可以响应任意类型的数据,这里为了演示方便就还是用的jsonres.SendErrorMsg(1128,result)return}//可以响应任意类型的数据,这里为了演示方便就还是用的jsonres.SendJson(`{"msg":"SUCCESS"}`)})}//接收参数的实体typeDemoParamstruct{TestStringReceptionstring`notnull:"true"msg:"TestStringReceptionCannotbeempty"routes:"/example/put"`TestIntReceptionint`max:"123"min:"32"msg:"TestIntReceptionThevaluerangemustbebetween32-123"routes:"/example/post"`TestUintReceptionuint`max:"123"min:"32"msg:"TestUintReceptionThevaluerangemustbebetween32-123"`TestFloatReceptionfloat32`max:"123"min:"32"msg:"TestFloatReceptionThevaluerangemustbebetween32-123"`TestBoolReceptionboolTestStringRegReceptionstring`reg:"^[a-z]+$"msg:"TestStringRegReceptionDoesnotmeettheregular"`TestBeeFileReceptioncommons.BeeFileTestJsonReception[]string}WebSocket示例创建一个函数来管理WebSocket路由

funcCreateWebSocketRoute(){wroute.AddWebSocketRoute("/ws/test",onConnection,onMessage,onClose)wroute.AddWebSocketRoute("/ws/test2",onConnection,onMessage,onClose)}//Inordertosavetime,onlythreefunctionsareusedbelow.Inpractice,youcanconfigureasetoffunctionsforeachwroutefunconConnection(session*wparams.WebSocketSession,msgstring){session.SendString("connectionsuccess")}funconMessage(session*wparams.WebSocketSession,msgstring){session.SendString("Igotthemessage.")}funconClose(session*wparams.WebSocketSession,msgstring){println(msg+"-------------------------------")}

启动服务

funcmain(){//Interceptors,routes,etc.Loadingofdatarequiresitsowncallsroutes.CreateRoute()routes.CreateWebSocketRoute()//Listentheserviceandlistentoport8080beerus.ListenHTTP(8080)}

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

查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论