
Go 版本的后端万用模板分享~
前言
原本在鱼总刚做 Java 万用模板后就像开发一个了,最开始是用 gin 框架做了一个类似的模板,在做的过程中,我也在不断学习新的项目,学新的知识,回过头来看用 gin 开发的后端模板,感觉就是一坨屎山,我自己用起来都非常不舒服,整合各种东西也不方便,如果就用这个作为模板,那就违背了做模板的初衷了
而万用模板的初衷,是为了在开发项目的时候能迅速启动,加快开发的效率
就这样从大二上学期,拖到了大二下,也就是现在,在看鱼总开发新的鱼答答项目,用万用模板各种整合好的工具,以及 AI 的辅助下,几乎是瞬间完成了基础的增删改查的任务,而我每次开新的项目居然还要浪费大量的时间进行项目的初始化,这把我刺激的不轻,所以我又重启了开发 Go 版本的后端万用模板的道路
今天基本上是做出个雏形了,虽然功能略显简陋,但是该有的基本上都有了,麻雀虽小五脏俱全,之后的日子我也会继续去慢慢完善这个项目的,那废话不多说?分享一下我近期的成果:
GitHub 链接:基于 Kratos 框架实现的 Go Web 开发模板
项目介绍文档:
GoWeb 后端开发万用模板
模板特点
使用框架 & 插件
-
Kratos 作为 web 开发框架(同时支持 grpc)
-
JWT 作为认证工具(后续可能会改成 Session + Redis 的形式维持登录态)
-
Wire 作为依赖注入工具
-
Validate 作为参数校验工具
-
Protoc-gen-go 作为代码生成工具,加快开发效率
-
Gen 作为更友好 & 更安全 GORM 代码生成工具
数据存储
-
MySQL 数据库
-
Redis 内存数据库
后续考虑
-
腾讯云 COS 对象存储
-
Elasticsearch 搜索引擎
-
kafka 消息队列
业务特性
-
protocol 代码生成(通过写 proto 文件形式完成代码框架开发/项目配置/自定义错误码/参数校验 等等)
-
封装通用响应类 & 全局日志
-
openapi 接口文档
后续
-
使用 Session Redis 实现分布式登录
业务功能
-
用户登录、注册、检索、权限管理(后续改成 Session Redis 的分布式登录会添加登出功能)
-
帖子创建(后续会支持更多功能)
后续
-
完善用户相关功能
-
完善帖子相关功能
单元测试
-
暂无
架构设计
-
参考 Kratos 官方描述:Go工程化 - Project Layout 最佳实践 | Kratos (go-kratos.dev)
快速上手
1)下载依赖
依赖略多,先偷个懒
2)启动项目
kratos run
数据库表设计
(备注:Java 和 Golang 的数据库操作框架略有不同,所以数据库的设计并没有照搬鱼总的)
-- 用户表 create table if not exists users ( id bigint(32) unsigned not null auto_increment comment '主键', create_time timestamp not null default current_timestamp comment '创建时间', update_time timestamp not null default current_timestamp on update current_timestamp comment '更新时间', delete_at timestamp null default null comment '逻辑删除标记', account varchar(256) not null comment '账号', password varchar(512) not null comment '密码', username varchar(256) not null default '新用户' comment '用户昵称', avatar_url varchar(1024) not null default '' comment '用户头像', gender tinyint not null default '1' comment '性别:1男;2女;3隐藏', user_info varchar(1024) not null default '' comment '个人简介', user_role tinyint(4) not null default '10' comment '身份:10用户;20管理者;30封禁', primary key (`id`) ) engine=innodb default charset=utf8mb4 comment='用户表'; -- 帖子表 create table if not exists post ( id bigint auto_increment comment 'id' primary key, create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间', update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间', delete_at tinyint default 0 not null comment '是否删除', title varchar(512) not null comment '标题', content text not null comment '内容', tags varchar(1024) not null comment '标签列表(json 数组)', thumb_num int default 0 not null comment '点赞数', favour_num int default 0 not null comment '收藏数', user_id bigint not null comment '创建用户 id', index idx_userId (user_id) ) engine=innodb default charset=utf8mb4 comment '帖子'; -- 帖子点赞表 create table if not exists post_thumb ( id bigint auto_increment comment 'id' primary key, create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间', update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间', post_id bigint not null comment '帖子 id', user_id bigint not null comment '创建用户 id', index idx_postId (post_id), index idx_userId (user_id) ) engine=innodb default charset=utf8mb4 comment '帖子点赞'; -- 帖子收藏表 create table if not exists post_favour ( id bigint auto_increment comment 'id' primary key, create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间', update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间', post_id bigint not null comment '帖子 id', user_id bigint not null comment '创建用户 id', index idx_postId (post_id), index idx_userId (user_id) ) engine=innodb default charset=utf8mb4 comment '帖子收藏';
后续开发计划
文档暂时来说也比较简陋,大体是仿鱼总的万用模板的写法,后续也会慢慢完善的,所以接下来就说说后续的完善计划
1)中间件
文档中也有提到一些关于中间件后续计划,主要就是把主流的中间件都整合上,这样无论之后开发什么项目,都能有现成的东西用
2)业务逻辑
业务逻辑主要有两个方面,一个是用户相关的,为了整合 JWT 认证我花了不少的时间(Go 的解决方案还是比较让人头疼的),最后的结果我其实不太满意,所以后续我打算也用 Session + Redis 的用户登录态控制的方案
然后就是帖子相关的基础业务逻辑开发,这个就慢慢填充了,什么时候有空就开发一点,总归是能开发完的
3)文档编写
现在文档还不是很完善,之后项目的更新肯定是会同步更新文档的,不过后续我也有其他的计划
1、Kratos 框架 & 其他插件工具的使用教程
写一份帮助快速上手模板里面使用的一大堆奇奇怪怪的东西的教程
2、Docker 快速启动项目所需的环境
在开发这个模板的时候,我使用的各种中间件都不是本地的,全部都是直接在 Docker 快速启动起来的,非常的方便,后续可能会考虑写一份通过 Docker 快速启动这个模板所需中间件的教程
暂时的计划大概就是这样了
希望这个模板也能对大家有所帮助,后续我也会努力完善啦~
