Beerusgo 開發(fā)的 web 框架
Beerus 是 一個用 go 開發(fā)的 web 框架,屬于 Beerus 的子項目之一,底層基于 go 自帶的 net/http,在此基礎(chǔ)上擴展了很多功能,比如:
- 路由管理
- 會話管理
- 攔截器
- 實體接參數(shù)
- 參數(shù)自動化驗證
- WebSocket
文檔
示例
HTTP 示例
創(chuàng)建一個函數(shù)管理路由
func CreateRoute() {
// 打開JSON模式,默認(rèn)就是開啟的,這里為了演示 所以手工開了一下,實際是不需要的
route.JsonMode = true
// 任意請求方式,都可以像這樣 使用形參接收請求參數(shù)
// 路由函數(shù)必須有返回值,支持struct,map,數(shù)組,這里為了演示方便,直接用的map
route.GET("/example/get", func (param DemoParam) map[string]string{
// 直接返回即可,beerus會自動轉(zhuǎn)成json 響應(yīng)給前端
msg := make(map[string]string)
msg["msg"] = "success"
return msg
})
}
// 接收參數(shù)的實體
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
}
啟動服務(wù)
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() {
// 關(guān)閉JSON模式,默認(rèn)是開啟的,想要關(guān)閉 就必須手工設(shè)置為false
route.JsonMode = false
// 一樣支持形參接收參數(shù),但是路由函數(shù)不可以有返回值
route.POST("/example/post", func (param DemoParam, req commons.BeeRequest, res commons.BeeResponse) {
// 非JSON模式下,必須手工調(diào)用函數(shù) 完成參數(shù)驗證
var result = params.Validation(req, ¶m, param)
if result != params.SUCCESS {
// 可以響應(yīng)任意類型的數(shù)據(jù),這里為了演示方便 就還是用的json
res.SendErrorMsg(1128, result)
return
}
// 可以響應(yīng)任意類型的數(shù)據(jù),這里為了演示方便 就還是用的json
res.SendJson(`{"msg":"SUCCESS"}`)
})
}
// 接收參數(shù)的實體
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 示例
創(chuàng)建一個函數(shù)來管理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 + "-------------------------------")
}
啟動服務(wù)
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)
}
想了解更多的話,可以查閱我們的文檔哦
評論
圖片
表情
