您现在的位置是:首页 > 文章详情

Beerus 上线啦,用 Go 开发的 web 解决方案

日期:2021-12-13点击:519

Beerus是一个用 Go 开发的 web 解决方案,包含一个 web 框架,一个数据库操作框架,一个正在规划中的RPC框架,目前 web 框架和数据库操作框架已经发布了第一个版本。

Web框架

因为他是牵头的框架,说到web大家想到的肯定是接口管理,所以他的名字就直接沿用了Beerus,也就是这一套项目的品牌名称,它是以net/http 为基础,在此基础上扩展了路由的管理方式,并增加了拦截器,会话管理,用struct接收参数,参数验证等功能,还提供了WebSocket支持,可以将http协议升级到WebSocket 并实现通信

数据库操作框架

名字叫Beerus-DB,用到了[go-sql-driver/mysql]来做数据库连接与基础操作,在此基础上做了很多扩展,比如:连接池管理,多数据源,事务管理,单表无sql操作,多表以及复杂操作可以自己写sql,sql支持{}占位符,可以用struct作为参数来操作数据库等

示例

HTTP 示例

创建一个函数管理路由

 func CreateRoute() { // post route example route.POST("/example/post", func (req *commons.BeeRequest, res *commons.BeeResponse) { res.SendJson(`{"msg":"SUCCESS"}`) }) // get route example route.GET("/example/get", func (req *commons.BeeRequest, res *commons.BeeResponse) { res.SendJson(`{"msg":"SUCCESS"}`) }) }

启动服务

 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) }

如果你想用实体接收参数,可以这么做

 func CreateRoute() { // Example of parameter conversion to struct and parameter checksum route.POST("/example/post", func (req *commons.BeeRequest, res *commons.BeeResponse) {         // 首先需要建一个实体的实例         param := DemoParam{} // 调用这个函数,将请求的参数全部提取到实体中,支持任意请求方式 params.ToStruct(req, &param, param) // 对参数进行验证,如果没通过就返回 提示信息 var result = params.Validation(req, &param, param) if result != params.SUCCESS { res.SendErrorMsg(1128, result) return } // 如果你觉得上面的麻烦,那么也可以这样,直接采用一步到位的方式:参数提取 + 验证 var result = params.ToStructAndValidation(req, &param, param) if result != params.SUCCESS { res.SendErrorMsg(1128, result) return } res.SendJson(`{"msg":"SUCCESS"}`) }) } // DemoParam If you have a struct like this, and you want to put all the parameters from the request into this struct 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) }

单表操作

根据条件查询单表数据

 conditions := make([]*entity.Condition,0) conditions = append(conditions, &entity.Condition{Key:"id > ?", Val: 10}) conditions = append(conditions, &entity.Condition{Key:"and user_name = ?", Val: "bee"}) conditions = append(conditions, &entity.Condition{Key: "order by create_time desc", Val: entity.NotWhere}) resultMap, err := operation.GetDBTemplate("Data source name").Select("table name", conditions)

根据条件修改单表数据

 // 条件设定 conditions := make([]*entity.Condition,0) conditions = append(conditions, &entity.Condition{Key:"id = ?", Val: 1}) // 要修改的数据设定 data := ResultStruct{UserName: "TestNoSqlUpdate"} // 执行修改操作 result, err := operation.GetDBTemplate("Data source name").Update("table name", dbutil.StructToMapIgnore(&data, data, true),conditions)

根据条件删除单表数据

 // 设定删除条件 conditions := make([]*entity.Condition,0) conditions = append(conditions, &entity.Condition{Key:"id = ?", Val: 2}) // 执行删除操作 _, err := operation.GetDBTemplate("Data source name").Delete("table name", conditions)

插入一条数据

 data := ResultStruct{ UserName: "TestNoSqlInsert", UserEmail: "xxxxx@163.com", UpdateTime: "2021-12-09 13:50:00", } result, err := operation.GetDBTemplate("Data source name").Insert("table name", dbutil.StructToMapIgnore(&data, data, true))

自定义sql

查询

 // struct参数 res := ResultStruct{Id: 1} // 查多条, 注:这里需要用到占位符 resultMap, err := operation.GetDBTemplate("Data source name").SelectListByMap("select * from xt_message_board where id < {id}", dbutil.StructToMap(&res, res)) // 查一条, 注:这里需要用到占位符 resultMap, err := operation.GetDBTemplate("Data source name").SelectOneByMap("select * from xt_message_board where id < {id}", dbutil.StructToMap(&res, res))

增删改

 res := ResultStruct{Id: 1, UserName: "TestUpdateByMap"} // 无论是增删改,都是调用ExecByMap函数,将sql和参数传入即可,注:这里需要用到占位符 operation.GetDBTemplate("Data source name").ExecByMap("update xt_message_board set user_name = {user_name} where id = {id}", dbutil.StructToMap(&res, res))

分页查询

 data := ResultStruct{ UserName: "TestNoSqlInsert", UserEmail: "xxxxx@163.com", } // 创建分页参数 param := entity.PageParam{ CurrentPage: 1, // 第几页 PageSize: 20, // 每页多少条 Params: dbutil.StructToMap(&data, data), // 查询参数 } // 执行查询操作 result, err := operation.GetDBTemplate("Data source name").SelectPage("select * from xt_message_board where user_name = {user_name} and user_email = {user_email}", param)

事务管理

 // 开启事务 id, err := db.Transaction() if err != nil { t.Error("TestUpdateTx: " + err.Error()) return } res := ResultStruct{Id: 1, UserName: "TestUpdateTx"} // 注:这里使用的不是GetDBTemplate,ExecByMap,而是 GetDBTemplateTx 和 ExecByTxMap // 使用事务和不使用事务,在调用的函数上,区别就是多了个Tx ss, err := operation.GetDBTemplateTx(id, "dbPoolTest").ExecByTxMap("update xt_message_board set user_name = {user_name} where id = {id}", dbutil.StructToMap(&res, res)) if err != nil { // 如果有问题就回滚事务 db.Rollback(id) t.Error("TestUpdateTx: " + err.Error()) return } // 提交事务 db.Commit(id)

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

https://beeruscc.com

原文链接:https://www.oschina.net/news/173558/beerus-on-line
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章