Beerus 上线啦,用 Go 开发的 web 解决方案
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, ¶m, param) // 对参数进行验证,如果没通过就返回 提示信息 var result = params.Validation(req, ¶m, param) if result != params.SUCCESS { res.SendErrorMsg(1128, result) return } // 如果你觉得上面的麻烦,那么也可以这样,直接采用一步到位的方式:参数提取 + 验证 var result = params.ToStructAndValidation(req, ¶m, 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)
想了解更多的话,可以查阅我们的文档哦

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
淘系前端架构周刊:ahooks 3.0 发布、Tailwind CSS v3.0
昭昭前事,惕惕后人。 🗞 News ahooks 3.0 发布 ahooks 作为 React 社区中优质可靠的 Hooks 库,经过近两年多的迭代,在国内外社区都获得了很多同学的认可,时至今日 ahooks 发布 3.0 版本,为我们带来了以下新特性: 全面支持 SSR 全新的 useRequest 所有的输出函数引用是固定的,避免闭包问题 DOM 类 Hooks 支持 target 动态变化 更合理的 API 设计 解决了在严格模式(Strict Mode)下的问题 解决了在 react-refresh(HMR)模式下的问题 新增了更多 Hooks &etc. Release Blog:ahooks 3.0 来了!高质量可靠的 React Hooks 库 - 知乎 (zhihu.com) Home Page:ahooks - React Hooks Library - ahooks 3.0 GitHub Repo:GitHub - alibaba/hooks: A high-quality & reliable React Hooks library. Tailw...
- 下一篇
Pear Admin Ant 3.0.1 发布,完成 ant design vue 3.0 兼容支持
Pear Admin Ant , 基于 Ant Design Vue 3.0.0 的后台管理框架 更新内容: [支持] 完成 ant design vue 3.0.0-alpha.13 适配 [新增] p-icon-pciker 组件 name 属性,原生 input name 值。 [新增] p-query 查询组件 type 属性 custom 值,支持自定义插槽 [新增] p-table 表格组件,defaultToolbar 属性,支持关闭工具栏 [新增] p-table 表格组件,自定义插槽 [修复] p-icon-picker 组件 value 属性为 modalValue。 [修复] p-icon-pciker 组件 update:modelValue emit 缺失所产生的警告。 [修复] menu 菜单当 children 转为一级菜单。 [修复] 菜单,主题,多视图,切换不保存 [修复] 现存 warming 警告 [升级] vue 3.2.23 [升级] vite-plugin-theme-preprocessor 1.3.3 [升级] plugin-vue 1....
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作