Skip to content

Commit 5ada9ce

Browse files
authored
feat: 数据库类型支持sqlite3 (#1)
1 parent d0f488b commit 5ada9ce

14 files changed

+73
-324
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
build.sh
1212
logs
1313
xirang
14+
xirang.db
1415

1516
# Test binary, built with `go test -c`
1617
*.test

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
只需要根据情况修改配置`config.yml`,然后配置里边的数据库配置信息,即可开始开发。
3737

38-
数据表会自动创建,也可以通过docs下的sql自行创建
38+
数据库支持MySQL与sqlite3,如果你的系统仅为运维内部一个小系统,则推荐你使用sqlite3。数据表会自动映射并创建
3939

4040
## 👨‍💻 项目地址
4141

config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ logs:
2727
# 是否压缩
2828
compress: false
2929

30+
database:
31+
# 数据库类型 mysql/sqlite3
32+
driver: sqlite3
33+
# 数据库连接sqlite3数据文件的路径
34+
source: xirang.db
35+
3036
mysql:
3137
# 用户名
3238
username: root

config/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var Conf = new(config)
1818
type config struct {
1919
System *SystemConfig `mapstructure:"system" json:"system"`
2020
Logs *LogsConfig `mapstructure:"logs" json:"logs"`
21+
Database *Database `mapstructure:"database" json:"database"`
2122
Mysql *MysqlConfig `mapstructure:"mysql" json:"mysql"`
2223
Casbin *CasbinConfig `mapstructure:"casbin" json:"casbin"`
2324
Jwt *JwtConfig `mapstructure:"jwt" json:"jwt"`
@@ -99,6 +100,11 @@ type LogsConfig struct {
99100
Compress bool `mapstructure:"compress" json:"compress"`
100101
}
101102

103+
type Database struct {
104+
Driver string `mapstructure:"driver" json:"driver"`
105+
Source string `mapstructure:"source" json:"source"`
106+
}
107+
102108
type MysqlConfig struct {
103109
Username string `mapstructure:"username" json:"username"`
104110
Password string `mapstructure:"password" json:"password"`

go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ require (
1818
go.uber.org/zap v1.19.1
1919
gopkg.in/natefinch/lumberjack.v2 v2.0.0
2020
gorm.io/driver/mysql v1.3.2
21-
gorm.io/gorm v1.23.6
21+
gorm.io/driver/sqlite v1.3.6
22+
gorm.io/gorm v1.23.8
2223
)
2324

2425
require (
2526
github.com/BurntSushi/toml v1.1.0 // indirect
2627
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect
2728
github.com/golang/mock v1.6.0 // indirect
2829
github.com/lib/pq v1.10.4 // indirect
30+
github.com/mattn/go-sqlite3 v1.14.15 // indirect
2931
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3032
github.com/modern-go/reflect2 v1.0.2 // indirect
3133
github.com/pelletier/go-toml/v2 v2.0.0 // indirect
@@ -53,7 +55,7 @@ require (
5355
github.com/jackc/pgtype v1.10.0 // indirect
5456
github.com/jackc/pgx/v4 v4.15.0 // indirect
5557
github.com/jinzhu/inflection v1.0.0 // indirect
56-
github.com/jinzhu/now v1.1.4 // indirect
58+
github.com/jinzhu/now v1.1.5 // indirect
5759
github.com/json-iterator/go v1.1.12 // indirect
5860
github.com/leodido/go-urn v1.2.1 // indirect
5961
github.com/magiconair/properties v1.8.6 // indirect

go.sum

+10-296
Large diffs are not rendered by default.

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
common.InitLogger()
2525

2626
// 初始化数据库(mysql)
27-
common.InitMysql()
27+
common.InitDB()
2828

2929
// 初始化casbin策略管理器
3030
common.InitCasbinEnforcer()

middleware/OperationLogMiddleware.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middleware
22

33
import (
4+
"fmt"
45
"strings"
56
"time"
67

@@ -56,9 +57,9 @@ func OperationLogMiddleware() gin.HandlerFunc {
5657
Path: path,
5758
Remark: api.Remark,
5859
Status: c.Writer.Status(),
59-
StartTime: startTime,
60+
StartTime: fmt.Sprintf("%v", startTime),
6061
TimeCost: timeCost,
61-
//UserAgent: c.Request.UserAgent(),
62+
UserAgent: c.Request.UserAgent(),
6263
}
6364

6465
// 最好是将日志发送到rabbitmq或者kafka中

model/menu.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Menu struct {
1212
Path string `gorm:"type:varchar(100);comment:'菜单访问路径'" json:"path"`
1313
Redirect string `gorm:"type:varchar(100);comment:'重定向路径'" json:"redirect"`
1414
Component string `gorm:"type:varchar(100);comment:'前端组件路径'" json:"component"`
15-
Sort uint `gorm:"type:int(3) unsigned;default:999;comment:'菜单顺序(1-999)'" json:"sort"`
15+
Sort uint `gorm:"type:int(3);default:999;comment:'菜单顺序(1-999)'" json:"sort"`
1616
Status uint `gorm:"type:tinyint(1);default:1;comment:'菜单状态(正常/禁用, 默认正常)'" json:"status"`
1717
Hidden uint `gorm:"type:tinyint(1);default:2;comment:'菜单在侧边栏隐藏(1隐藏,2显示)'" json:"hidden"`
1818
NoCache uint `gorm:"type:tinyint(1);default:2;comment:'菜单是否被 <keep-alive> 缓存(1不缓存,2缓存)'" json:"noCache"`

model/operation_log.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package model
22

33
import (
4-
"time"
5-
64
"gorm.io/gorm"
75
)
86

97
type OperationLog struct {
108
gorm.Model
11-
Username string `gorm:"type:varchar(20);comment:'用户登录名'" json:"username"`
12-
Ip string `gorm:"type:varchar(20);comment:'Ip地址'" json:"ip"`
13-
IpLocation string `gorm:"type:varchar(20);comment:'Ip所在地'" json:"ipLocation"`
14-
Method string `gorm:"type:varchar(20);comment:'请求方式'" json:"method"`
15-
Path string `gorm:"type:varchar(100);comment:'访问路径'" json:"path"`
16-
Remark string `gorm:"type:varchar(100);comment:'备注'" json:"remark"`
17-
Status int `gorm:"type:int(4);comment:'响应状态码'" json:"status"`
18-
StartTime time.Time `gorm:"type:datetime(3);comment:'发起时间'" json:"startTime"`
19-
TimeCost int64 `gorm:"type:int(6);comment:'请求耗时(ms)'" json:"timeCost"`
20-
UserAgent string `gorm:"type:varchar(20);comment:'浏览器标识'" json:"userAgent"`
9+
Username string `gorm:"type:varchar(20);comment:'用户登录名'" json:"username"`
10+
Ip string `gorm:"type:varchar(20);comment:'Ip地址'" json:"ip"`
11+
IpLocation string `gorm:"type:varchar(20);comment:'Ip所在地'" json:"ipLocation"`
12+
Method string `gorm:"type:varchar(20);comment:'请求方式'" json:"method"`
13+
Path string `gorm:"type:varchar(100);comment:'访问路径'" json:"path"`
14+
Remark string `gorm:"type:varchar(100);comment:'备注'" json:"remark"`
15+
Status int `gorm:"type:int(4);comment:'响应状态码'" json:"status"`
16+
StartTime string `gorm:"type:varchar(2048);comment:'发起时间'" json:"startTime"`
17+
TimeCost int64 `gorm:"type:int(6);comment:'请求耗时(ms)'" json:"timeCost"`
18+
UserAgent string `gorm:"type:varchar(2048);comment:'浏览器标识'" json:"userAgent"`
2119
}

public/common/database.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,38 @@ import (
77
"github.com/eryajf/xirang/model"
88

99
"gorm.io/driver/mysql"
10+
"gorm.io/driver/sqlite"
1011
"gorm.io/gorm"
1112
)
1213

13-
// 全局mysql数据库变量
14+
// 全局数据库对象
1415
var DB *gorm.DB
1516

16-
// 初始化mysql数据库
17-
func InitMysql() {
17+
// 初始化数据库
18+
func InitDB() {
19+
if config.Conf.Database.Driver == "mysql" {
20+
initMysql()
21+
} else if config.Conf.Database.Driver == "sqlite3" {
22+
initSqlite3()
23+
}
24+
}
25+
26+
func initSqlite3() {
27+
db, err := gorm.Open(sqlite.Open(config.Conf.Database.Source+"?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{
28+
// 禁用外键(指定外键时不会在mysql创建真实的外键约束)
29+
DisableForeignKeyConstraintWhenMigrating: true,
30+
})
31+
if err != nil {
32+
Log.Panicf("failed to connect sqlite3: %v", err)
33+
}
34+
35+
DB = db
36+
// 自动迁移表结构
37+
dbAutoMigrate()
38+
Log.Infof("初始化 sqlite3 数据库完成!")
39+
}
40+
41+
func initMysql() {
1842
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&collation=%s&%s",
1943
config.Conf.Mysql.Username,
2044
config.Conf.Mysql.Password,
@@ -40,10 +64,6 @@ func InitMysql() {
4064
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
4165
// 禁用外键(指定外键时不会在mysql创建真实的外键约束)
4266
DisableForeignKeyConstraintWhenMigrating: true,
43-
//// 指定表前缀
44-
//NamingStrategy: schema.NamingStrategy{
45-
// TablePrefix: config.Conf.Mysql.TablePrefix + "_",
46-
//},
4767
})
4868
if err != nil {
4969
Log.Panicf("初始化mysql数据库异常: %v", err)

public/common/init_mysql_data.go

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func InitData() {
117117
Component: "/personnel/group/index",
118118
Sort: 7,
119119
ParentId: uint1,
120+
NoCache: 1,
120121
Roles: roles[:1],
121122
Creator: "系统",
122123
},

service/isql/operation_log_isql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.Operation
4848
// List 获取数据列表
4949
func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.OperationLog, error) {
5050
var list []*model.OperationLog
51-
db := common.DB.Model(&model.OperationLog{}).Order("start_time DESC")
51+
db := common.DB.Model(&model.OperationLog{}).Order("id DESC")
5252

5353
username := strings.TrimSpace(req.Username)
5454
if username != "" {

test/isql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func InitConfig() {
1818
common.InitLogger()
1919

2020
// 初始化数据库(mysql)
21-
common.InitMysql()
21+
common.InitDB()
2222

2323
// 初始化casbin策略管理器
2424
common.InitCasbinEnforcer()

0 commit comments

Comments
 (0)