Beerusgo 开发的 web 框架
Beerus 是 一个用 go 开发的 web 框架,属于 Beerus 的子项目之一,底层基于 go 自带的 net/http,在此基础上扩展了很多功能,比如:
- 路由管理
- 会话管理
- 拦截器
- 实体接参数
- 参数自动化验证
- WebSocket
文档
示例
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, ¶m, 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)
}
想了解更多的话,可以查阅我们的文档哦
评论
dogo-frameworkGo 开发的 Web 框架
dogo,用Go语言开发的Web框架,高效简单。采用微内核框架,高度可定制化。可以很方便添加各种组件。package mainimport ( "github.com/wuciyou/dogo"
dogo-frameworkGo 开发的 Web 框架
0
WebSharperF#的Web开发框架
WebSharper,它是一个基于F#构建的Web开发平台,使用F#构造从前到后的一整套内容。其中利用到F#中许多高级的开发特性,并可以将F#代码直接转化JavaScript,这样服务器端和客户端的通
WebSharperF#的Web开发框架
0
CppCMSC++的Web开发框架
CppCMS是一个C++的Web开发框架(不是一个CMS)。它不同于大多数其他Web开发框架,如巨蟒Django,Java的Servlets,或C++Wt因为它在设计和调整来处理极高的负荷,而且它的目
CppCMSC++的Web开发框架
0
UiUIKitiPhone的web开发框架
UiUIKit(UniversaliPhoneUIKit)是一個模擬iPhonenativeUI的HTML/CSS3framework,並提供原始的PSD影像檔。
UiUIKitiPhone的web开发框架
0
JCatapultJava的Web开发框架
JCatapult是一个新的用于开发基于Java的Web应用的框架,基于很多工业标准,包括J2EE、JPA以及一些事实上的标准框架,有如Ant和Hibernate、Guice、Joda-Time等。
JCatapultJava的Web开发框架
0