前言
最近两个月一直在忙公司的项目,上班时间经常高强度写代码,下班了只想躺着,没心思再学习、做自己的项目了。最近这几天轻松一点了,终于有时间 摸鱼了 做自己的事了,所以到现在我总算是搭起来一个比较完整的后台管理系统了。
框架
我的go版本是1.21版本的。
web框架:GoFiber
用的web框架是GoFiber,我在网上找基于GoFiber的web项目,都很少,大部分用的都是Gin。
go get github.com/gofiber/fiber/v2
数据库框架:Gorm
数据库用的是gorm。
go get gorm.io/gorm
缓存框架:Go-Redis
缓存用redis,框架用的 go-redis。
go get github.com/go-redis/redis
go.mod
go 1.21require (github.com/dlclark/regexp2 v1.10.0github.com/go-redis/redis v6.15.9+incompatiblegithub.com/gofiber/fiber/v2 v2.49.0github.com/google/uuid v1.3.1github.com/mojocn/base64Captcha v1.3.5github.com/mozillazg/go-pinyin v0.20.0github.com/pkg/errors v0.9.1github.com/robfig/cron v1.2.0github.com/spf13/viper v1.16.0golang.org/x/crypto v0.14.0gorm.io/driver/mysql v1.5.1gorm.io/gorm v1.25.3
)require (github.com/andybalholm/brotli v1.0.5 // indirectgithub.com/fsnotify/fsnotify v1.6.0 // indirectgithub.com/go-sql-driver/mysql v1.7.0 // indirectgithub.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirectgithub.com/hashicorp/hcl v1.0.0 // indirectgithub.com/jinzhu/inflection v1.0.0 // indirectgithub.com/jinzhu/now v1.1.5 // indirectgithub.com/klauspost/compress v1.16.7 // indirectgithub.com/magiconair/properties v1.8.7 // indirectgithub.com/mattn/go-colorable v0.1.13 // indirectgithub.com/mattn/go-isatty v0.0.19 // indirectgithub.com/mattn/go-runewidth v0.0.15 // indirectgithub.com/mitchellh/mapstructure v1.5.0 // indirectgithub.com/onsi/ginkgo v1.16.5 // indirectgithub.com/onsi/gomega v1.30.0 // indirectgithub.com/pelletier/go-toml/v2 v2.0.8 // indirectgithub.com/rivo/uniseg v0.4.4 // indirectgithub.com/spf13/afero v1.9.5 // indirectgithub.com/spf13/cast v1.5.1 // indirectgithub.com/spf13/jwalterweatherman v1.1.0 // indirectgithub.com/spf13/pflag v1.0.5 // indirectgithub.com/subosito/gotenv v1.4.2 // indirectgithub.com/valyala/bytebufferpool v1.0.0 // indirectgithub.com/valyala/fasthttp v1.48.0 // indirectgithub.com/valyala/tcplisten v1.0.0 // indirectgolang.org/x/image v0.11.0 // indirectgolang.org/x/sys v0.13.0 // indirectgolang.org/x/text v0.13.0 // indirectgopkg.in/ini.v1 v1.67.0 // indirectgopkg.in/yaml.v3 v3.0.1 // indirect
)
项目结构
我们先来看看项目结构:
还漏了个 router.go ,这个文件就是统一的GoFiber路由管理。
模块
看上面的图,就能知道这个后台管理系统有哪些模块了:
- 登录退出
- 日志管理
- 安全设置
- 用户管理
- 部门管理
- 角色管理
- 菜单管理
- 字典管理
其中日志管理包含了登录日志、操作日志(增、删、改);角色和菜单管理包含了按钮权限;用户、部门包含了数据权限(过滤)。
数据库
mysql数据库,主要有下面几张表:
SQL
可复制下面的代码直接执行,执行完成后,用户表的两个用户的密码是:123456
DROP TABLE IF EXISTS `sys_dept`;
CREATE TABLE `sys_dept` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'id',`creator_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',`parent_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '父部门id',`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '部门名称',`level` tinyint(1) NULL DEFAULT NULL COMMENT '层级(1 根目录 2 单位 3 部门 4 小组)',`sort` int NULL DEFAULT NULL COMMENT '序号',PRIMARY KEY (`id`) USING BTREE,INDEX `name`(`name` ASC) USING BTREE,INDEX `parent_id`(`parent_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '部门管理' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_dept` VALUES ('2', NULL, '2023-11-01 09:28:59', NULL, '2023-11-01 09:28:59', 'ROOT', 'A公司', 1, 2);
INSERT INTO `sys_dept` VALUES ('3', NULL, '2023-11-01 09:30:28', NULL, '2023-11-01 09:30:28', '2', '办公室', 2, 1);
INSERT INTO `sys_dept` VALUES ('4', NULL, '2023-11-01 09:30:37', NULL, '2023-11-01 09:30:37', '3', '开发部', 3, 1);
INSERT INTO `sys_dept` VALUES ('6ad201240ab74b02b57b7eb3caadcd49', NULL, '2023-05-10 15:24:53', NULL, '2023-05-10 15:24:53', '3', '财务部', 4, 2);DROP TABLE IF EXISTS `sys_dict`;
CREATE TABLE `sys_dict` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'id',`parent_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '父级id',`dict_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '字典名称',`dict_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '字典代码',`dict_value` int NULL DEFAULT NULL COMMENT '字典值',`is_type` tinyint(1) NULL DEFAULT NULL COMMENT '是否是字典类型(1 字典类型 2 字典项)',`sort` int NULL DEFAULT NULL COMMENT '排序',`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',`creator_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE,INDEX `dict_code`(`dict_code` ASC) USING BTREE,INDEX `dict_name`(`dict_name` ASC) USING BTREE,INDEX `parent_id`(`parent_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '字典管理' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_dict` VALUES ('12baffa6f2c84d1db39d55d7a0926d27', 'aae7ec823bbc42198480a01f2c60463e', '测试_2', 'CS_2', 2, 2, 2, '666', NULL, '2023-12-05 16:36:47', NULL, NULL);
INSERT INTO `sys_dict` VALUES ('aae7ec823bbc42198480a01f2c60463e', 'ROOT', '测试', 'CS', NULL, 1, NULL, NULL, 'e8b56a16283240478afe42c38065a4da', '2023-10-13 12:02:27', NULL, '2023-10-13 12:02:27');
INSERT INTO `sys_dict` VALUES ('f4e3fb5e1e554cd6b0cd8a292ea94fa8', 'aae7ec823bbc42198480a01f2c60463e', '测试_1', 'CS_1', 1, 2, 1, '666', NULL, '2023-12-05 16:36:38', NULL, NULL);DROP TABLE IF EXISTS `sys_log`;
CREATE TABLE `sys_log` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`creator_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',`ip` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户ip',`title` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '标题,操作方法描述',`type` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '操作类型(其他 登录 退出 新增 修改 删除 导入 导出 设置状态 设置密码)',`method` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '操作方法',`url` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '请求url',`info` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '详情',`state` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '状态(操作成功 操作失败)',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '日志管理' ROW_FORMAT = DYNAMIC;DROP TABLE IF EXISTS `sys_menu`;
CREATE TABLE `sys_menu` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单ID',`parent_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '父菜单ID',`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单名称',`sort` int NULL DEFAULT 0 COMMENT '显示顺序',`url` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '访问路径',`path` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '组件名称',`type` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '菜单类型(M目录 C菜单 F按钮)',`state` tinyint(1) NULL DEFAULT 1 COMMENT '菜单状态(1正常 2停用 3删除)',`perms` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '权限标识',`visible` tinyint(1) NULL DEFAULT 0 COMMENT '显示状态(0隐藏 1显示)',`icon` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '#' COMMENT '菜单图标',`active_menu` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '菜单高亮',`is_frame` tinyint(1) NULL DEFAULT NULL COMMENT '是否外链(0 否 1 是)',`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '备注',`creator_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '菜单权限表' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_menu_copy1` VALUES ('1', 'ROOT', '系统设置', 12, 'ParentView', 'system', 'M', 1, 'system', 0, 'system', NULL, NULL, NULL, NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('10', '2', '修改', 2, NULL, NULL, 'F', 1, 'system:user:update', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('11', '2', '删除', 3, NULL, NULL, 'F', 1, 'system:user:delete', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('12', '2', '修改密码', 4, NULL, NULL, 'F', 1, 'system:user:updatePassword', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('16', '4', '新增', 1, NULL, NULL, 'F', 1, 'system:role:add', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('17', '4', '修改', 2, NULL, NULL, 'F', 1, 'system:role:update', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('18', '4', '删除', 3, NULL, NULL, 'F', 1, 'system:role:delete', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('19', '5', '新增', 1, NULL, NULL, 'F', 1, 'system:menu:add', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('2', '1', '用户管理', 1, '/system/user', 'user', 'C', 1, 'system:user:view', 0, 'user', NULL, NULL, NULL, NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('20', '5', '修改', 2, NULL, NULL, 'F', 1, 'system:menu:update', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('21', '5', '删除', 3, NULL, NULL, 'F', 1, 'system:menu:delete', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('22', '6', '新增', 1, NULL, NULL, 'F', 1, 'system:dict:add', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('23', '6', '修改', 2, NULL, NULL, 'F', 1, 'system:dict:update', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('24', '6', '删除', 3, NULL, NULL, 'F', 1, 'system:dict:delete', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('25', '7', '修改', 1, NULL, NULL, 'F', 1, 'system:safe:update', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('4', '1', '角色管理', 2, '/system/role', 'role', 'C', 1, 'system:role:view', 0, 'role', NULL, NULL, NULL, NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('5', '1', '菜单管理', 3, '/system/menu', 'menu', 'C', 1, 'system:menu:view', 0, 'menu', NULL, NULL, NULL, NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('6', '1', '字典管理', 4, '/system/dict', 'dict', 'C', 1, 'system:dict:view', 0, 'dict', NULL, NULL, NULL, NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('7', '1', '安全设置', 5, '/system/safe', 'safe', 'C', 1, 'system:safe:view', 0, 'safe', NULL, NULL, '', NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('8', '1', '系统日志', 6, '/system/userLog', 'userLog', 'C', 1, 'system:userLog:view', 0, 'log', NULL, NULL, '', NULL, '2022-09-14 13:54:24', NULL, NULL);
INSERT INTO `sys_menu_copy1` VALUES ('9', '2', '新增', 1, NULL, NULL, 'F', 1, 'system:user:add', 0, '#', NULL, NULL, NULL, NULL, NULL, NULL, NULL);DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '角色ID',`role_key` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '角色权限字符',`role_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '角色名称',`is_open` tinyint(1) NULL DEFAULT 1 COMMENT '菜单树是否展开(0折叠 1展开 )',`state` tinyint(1) NULL DEFAULT 0 COMMENT '角色状态(1正常 2停用 3删除)',`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',`creator_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '角色管理' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_role` VALUES ('1', 'CJGLY', '超级管理员', 1, 1, NULL, NULL, '2022-09-14 14:25:07', NULL, '2022-09-14 14:25:07');
INSERT INTO `sys_role` VALUES ('2', 'PTYH', '普通用户', 1, 1, NULL, NULL, '2022-09-14 14:38:35', NULL, '2022-09-14 14:38:35');DROP TABLE IF EXISTS `sys_role_menu`;
CREATE TABLE `sys_role_menu` (`role_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '角色ID',`menu_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单ID',PRIMARY KEY (`role_id`, `menu_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '角色菜单关联' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_role_menu` VALUES ('1', '1');
INSERT INTO `sys_role_menu` VALUES ('1', '10');
INSERT INTO `sys_role_menu` VALUES ('1', '11');
INSERT INTO `sys_role_menu` VALUES ('1', '12');
INSERT INTO `sys_role_menu` VALUES ('1', '16');
INSERT INTO `sys_role_menu` VALUES ('1', '17');
INSERT INTO `sys_role_menu` VALUES ('1', '18');
INSERT INTO `sys_role_menu` VALUES ('1', '19');
INSERT INTO `sys_role_menu` VALUES ('1', '2');
INSERT INTO `sys_role_menu` VALUES ('1', '20');
INSERT INTO `sys_role_menu` VALUES ('1', '21');
INSERT INTO `sys_role_menu` VALUES ('1', '22');
INSERT INTO `sys_role_menu` VALUES ('1', '23');
INSERT INTO `sys_role_menu` VALUES ('1', '24');
INSERT INTO `sys_role_menu` VALUES ('1', '25');
INSERT INTO `sys_role_menu` VALUES ('1', '4');
INSERT INTO `sys_role_menu` VALUES ('1', '5');
INSERT INTO `sys_role_menu` VALUES ('1', '6');
INSERT INTO `sys_role_menu` VALUES ('1', '7');
INSERT INTO `sys_role_menu` VALUES ('1', '8');
INSERT INTO `sys_role_menu` VALUES ('1', '9');DROP TABLE IF EXISTS `sys_safe`;
CREATE TABLE `sys_safe` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`creator_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',`pwd_cycle` int NULL DEFAULT NULL COMMENT '密码更改周期(90天,60天,30天,0无)',`pwd_login_limit` tinyint(1) NULL DEFAULT NULL COMMENT '密码登录限制(0:连续错3次,锁定账号15分钟。1:连续错5次,锁定账号30分钟)',`idle_time_setting` tinyint(1) NULL DEFAULT NULL COMMENT '闲置时间设置(0:无。1:空闲30分钟,系统默认用户退出)',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '安全设置' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_safe` VALUES ('1', NULL, '2023-04-13 14:23:33', NULL, '2023-04-13 14:23:33', 30, 0, 0);DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户编号',`creator_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',`create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',`update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名称',`real_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户姓名',`dept_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '所属部门',`role_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '所属角色',`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '手机号码',`password` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '密码',`state` tinyint(1) NULL DEFAULT 1 COMMENT '状态(1 启用 2 停用)',`picture` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '头像',PRIMARY KEY (`id`) USING BTREE,UNIQUE INDEX `user_name`(`user_name` ASC) USING BTREE,INDEX `dept_id`(`dept_id` ASC) USING BTREE,INDEX `role_id`(`role_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户管理' ROW_FORMAT = DYNAMIC;INSERT INTO `sys_user` VALUES ('d2c41c44f5f841c4a6b7902d223f5a6c', NULL, '2023-02-09 17:47:58', NULL, '2023-02-09 17:47:58', 'fuhua', '符华', '4', '1', NULL, '$2a$10$mDOA3K8OivAqs4i2iQJjoOVisn05FZJzSK96xGvWV4Db9FJC5zPSO', 1, NULL);
INSERT INTO `sys_user` VALUES ('e8b56a16283240478afe42c38065a4da', NULL, '2023-02-09 17:47:48', NULL, '2023-02-09 17:47:48', 'admin', '超管', '4', '1', NULL, '$2a$10$mDOA3K8OivAqs4i2iQJjoOVisn05FZJzSK96xGvWV4Db9FJC5zPSO', 1, NULL);
配置文件
项目配置、数据库连接、redis连接等信息,用yml文件配置,然后再用代码解析这个配置文件。
yml配置
# 项目配置
server:port: 7070read_timeout: 10write_timeout: 10# MySQL数据库连接配置
database:dbtype: mysqlhost: 127.0.0.1port: 3306username: rootpassword: rootdatabase: gorm_dbtimeout: 10s# redis连接配置
redis:host: 127.0.0.1port: 6379password: key#1234ip:# 设置IP白名单,多个用;号隔开。也可以用通配符 * 号,如:192.*.*.*auth_host: "*.*.*.*"# 设置允许跨域调用接口的api(这是前端请求IP和端口,不是后端服务器接口)allow_cors_api: "http://127.0.0.1:7070"# 文件上传路径
filePath: upload
解析yml
application.go
package configimport ("fmt""github.com/go-redis/redis""github.com/spf13/viper""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""gorm.io/gorm/schema""strings""time"
)var (Config *viper.ViperHTTPPort intReadTimeout time.DurationWriteTimeout time.DurationDB *gorm.DBRedisConn *redis.ClientAuthHost []stringAllowCorsApi stringFilePath string
)func InitConfig() (*viper.Viper, error) {viper.SetConfigName("application") // 配置文件的文件名(不带扩展名)viper.SetConfigType("yaml") // 配置文件的类型//viper.AddConfigPath("./app.common.config") // 配置文件所在的路径,这里假设配置文件在项目根目录下的 app.common.config 文件夹中viper.AddConfigPath("./") // 配置文件所在的路径,项目根目录下err := viper.ReadInConfig() // 读取配置文件if err != nil {return nil, err}Config = viper.GetViper()LoadServer()LoadMysql()LoadRedis()LoadIP()return Config, nil
}func LoadServer() {HTTPPort = Config.GetInt("server.port")ReadTimeout = time.Duration(Config.GetInt("server.read_timeout")) * time.SecondWriteTimeout = time.Duration(Config.GetInt("server.write_timeout")) * time.SecondFilePath = Config.GetString("filePath")
}func LoadMysql() {dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=%s",Config.Get("database.username"),Config.Get("database.password"),Config.Get("database.host"),Config.GetInt("database.port"),Config.Get("database.database"),Config.Get("database.timeout"))db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{// 跳过默认事务:为了确保数据一致性,GORM 会在事务里执行写入操作(创建、更新、删除)。如果没有这方面的要求,您可以在初始化时禁用它,这样可以获得60%的性能提升//SkipDefaultTransaction: true,Logger: logger.Default.LogMode(logger.Info), // sql全局日志NamingStrategy: schema.NamingStrategy{//TablePrefix: "sys_", // 表名前缀SingularTable: true, // 单数表名//NoLowerCase: false, // 关闭小写转换},})if err != nil {fmt.Println("无法连接到MySQL :", err)}DB = db
}func LoadRedis() {RedisConn = redis.NewClient(&redis.Options{Addr: fmt.Sprintf("%s:%d", Config.Get("redis.host"), Config.GetInt("redis.port")),Password: Config.GetString("redis.password"),})if err := RedisConn.Ping().Err(); err != nil {fmt.Println("无法连接到redis")}
}func LoadIP() {ips := Config.GetString("ip.auth_host")AuthHost = strings.Split(ips, ";")AllowCorsApi = Config.GetString("ip.allow_cors_api")
}
启动项目
有了数据库、解析好了配置文件,我们来试试启动项目
func main() {app := fiber.New()// 初始化yml配置_, err := config.InitConfig()if err != nil {panic(fmt.Errorf("加载yml配置文件错误: %s \n", err))}app.Listen(fmt.Sprintf(":%d", config.HTTPPort))
}
ok,以上就是本篇文章的全部内容了,等我更完这个项目的全部文章,我会放出完整代码的地址,欢迎大家多多点赞支持下,最后可以关注我不迷路~