feat: 数据库存储系统支持sqlite3 (#141)

This commit is contained in:
二丫讲梵 2022-12-30 14:50:59 +08:00 committed by GitHub
parent 8959e4949f
commit 0b73508fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 68 additions and 32 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ build.sh
logs logs
go-web-mini go-web-mini
go-ldap-admin go-ldap-admin
go-ldap-admin.db
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

View File

@ -27,6 +27,12 @@ logs:
# 是否压缩 # 是否压缩
compress: false compress: false
database:
# 数据库类型 mysql sqlite3
driver: mysql
# 数据库连接sqlite3数据文件的路径
source: go-ldap-admin.db
mysql: mysql:
# 用户名 # 用户名
username: root username: root

View File

@ -18,6 +18,7 @@ var Conf = new(config)
type config struct { type config struct {
System *SystemConfig `mapstructure:"system" json:"system"` System *SystemConfig `mapstructure:"system" json:"system"`
Logs *LogsConfig `mapstructure:"logs" json:"logs"` Logs *LogsConfig `mapstructure:"logs" json:"logs"`
Database *Database `mapstructure:"database" json:"database"`
Mysql *MysqlConfig `mapstructure:"mysql" json:"mysql"` Mysql *MysqlConfig `mapstructure:"mysql" json:"mysql"`
Casbin *CasbinConfig `mapstructure:"casbin" json:"casbin"` Casbin *CasbinConfig `mapstructure:"casbin" json:"casbin"`
Jwt *JwtConfig `mapstructure:"jwt" json:"jwt"` Jwt *JwtConfig `mapstructure:"jwt" json:"jwt"`
@ -104,6 +105,11 @@ type LogsConfig struct {
Compress bool `mapstructure:"compress" json:"compress"` Compress bool `mapstructure:"compress" json:"compress"`
} }
type Database struct {
Driver string `mapstructure:"driver" json:"driver"`
Source string `mapstructure:"source" json:"source"`
}
type MysqlConfig struct { type MysqlConfig struct {
Username string `mapstructure:"username" json:"username"` Username string `mapstructure:"username" json:"username"`
Password string `mapstructure:"password" json:"password"` Password string `mapstructure:"password" json:"password"`

6
go.mod
View File

@ -22,7 +22,8 @@ require (
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0
gorm.io/driver/mysql v1.3.2 gorm.io/driver/mysql v1.3.2
gorm.io/gorm v1.23.6 gorm.io/driver/sqlite v1.3.6
gorm.io/gorm v1.23.8
) )
require ( require (
@ -37,6 +38,7 @@ require (
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect
github.com/golang/mock v1.6.0 // indirect github.com/golang/mock v1.6.0 // indirect
github.com/lib/pq v1.10.4 // indirect github.com/lib/pq v1.10.4 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.0 // indirect github.com/pelletier/go-toml/v2 v2.0.0 // indirect
@ -67,7 +69,7 @@ require (
github.com/jackc/pgtype v1.10.0 // indirect github.com/jackc/pgtype v1.10.0 // indirect
github.com/jackc/pgx/v4 v4.15.0 // indirect github.com/jackc/pgx/v4 v4.15.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect github.com/magiconair/properties v1.8.6 // indirect

11
go.sum
View File

@ -378,8 +378,9 @@ github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@ -1097,8 +1098,9 @@ gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2
gorm.io/driver/postgres v1.0.1/go.mod h1:pv4dVhHvEVrP7k/UYqdBIllbdbpB5VTz89X1O0uOrCA= gorm.io/driver/postgres v1.0.1/go.mod h1:pv4dVhHvEVrP7k/UYqdBIllbdbpB5VTz89X1O0uOrCA=
gorm.io/driver/postgres v1.3.1 h1:Pyv+gg1Gq1IgsLYytj/S2k7ebII3CzEdpqQkPOdH24g= gorm.io/driver/postgres v1.3.1 h1:Pyv+gg1Gq1IgsLYytj/S2k7ebII3CzEdpqQkPOdH24g=
gorm.io/driver/postgres v1.3.1/go.mod h1:WwvWOuR9unCLpGWCL6Y3JOeBWvbKi6JLhayiVclSZZU= gorm.io/driver/postgres v1.3.1/go.mod h1:WwvWOuR9unCLpGWCL6Y3JOeBWvbKi6JLhayiVclSZZU=
gorm.io/driver/sqlite v1.3.1 h1:bwfE+zTEWklBYoEodIOIBwuWHpnx52Z9zJFW5F33WLk=
gorm.io/driver/sqlite v1.3.1/go.mod h1:wJx0hJspfycZ6myN38x1O/AqLtNS6c5o9TndewFbELg= gorm.io/driver/sqlite v1.3.1/go.mod h1:wJx0hJspfycZ6myN38x1O/AqLtNS6c5o9TndewFbELg=
gorm.io/driver/sqlite v1.3.6 h1:Fi8xNYCUplOqWiPa3/GuCeowRNBRGTf62DEmhMDHeQQ=
gorm.io/driver/sqlite v1.3.6/go.mod h1:Sg1/pvnKtbQ7jLXxfZa+jSHvoX8hoZA8cn4xllOMTgE=
gorm.io/driver/sqlserver v1.0.4/go.mod h1:ciEo5btfITTBCj9BkoUVDvgQbUdLWQNqdFY5OGuGnRg= gorm.io/driver/sqlserver v1.0.4/go.mod h1:ciEo5btfITTBCj9BkoUVDvgQbUdLWQNqdFY5OGuGnRg=
gorm.io/driver/sqlserver v1.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI= gorm.io/driver/sqlserver v1.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI=
gorm.io/driver/sqlserver v1.3.1/go.mod h1:w25Vrx2BG+CJNUu/xKbFhaKlGxT/nzRkhWCCoptX8tQ= gorm.io/driver/sqlserver v1.3.1/go.mod h1:w25Vrx2BG+CJNUu/xKbFhaKlGxT/nzRkhWCCoptX8tQ=
@ -1108,8 +1110,9 @@ gorm.io/gorm v1.20.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.2/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.2/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0= gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

View File

@ -26,7 +26,7 @@ func main() {
common.InitLogger() common.InitLogger()
// 初始化数据库(mysql) // 初始化数据库(mysql)
common.InitMysql() common.InitDB()
// 初始化ldap连接 // 初始化ldap连接
common.InitLDAP() common.InitLDAP()

View File

@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"fmt"
"strings" "strings"
"time" "time"
@ -56,9 +57,9 @@ func OperationLogMiddleware() gin.HandlerFunc {
Path: path, Path: path,
Remark: api.Remark, Remark: api.Remark,
Status: c.Writer.Status(), Status: c.Writer.Status(),
StartTime: startTime, StartTime: fmt.Sprintf("%v", startTime),
TimeCost: timeCost, TimeCost: timeCost,
//UserAgent: c.Request.UserAgent(), UserAgent: c.Request.UserAgent(),
} }
// 最好是将日志发送到rabbitmq或者kafka中 // 最好是将日志发送到rabbitmq或者kafka中

View File

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

View File

@ -1,8 +1,6 @@
package model package model
import ( import (
"time"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -15,7 +13,7 @@ type OperationLog struct {
Path string `gorm:"type:varchar(100);comment:'访问路径'" json:"path"` Path string `gorm:"type:varchar(100);comment:'访问路径'" json:"path"`
Remark string `gorm:"type:varchar(100);comment:'备注'" json:"remark"` Remark string `gorm:"type:varchar(100);comment:'备注'" json:"remark"`
Status int `gorm:"type:int(4);comment:'响应状态码'" json:"status"` Status int `gorm:"type:int(4);comment:'响应状态码'" json:"status"`
StartTime time.Time `gorm:"type:datetime(3);comment:'发起时间'" json:"startTime"` StartTime string `gorm:"type:varchar(2048);comment:'发起时间'" json:"startTime"`
TimeCost int64 `gorm:"type:int(6);comment:'请求耗时(ms)'" json:"timeCost"` TimeCost int64 `gorm:"type:int(6);comment:'请求耗时(ms)'" json:"timeCost"`
UserAgent string `gorm:"type:varchar(20);comment:'浏览器标识'" json:"userAgent"` UserAgent string `gorm:"type:varchar(2048);comment:'浏览器标识'" json:"userAgent"`
} }

View File

@ -7,14 +7,38 @@ import (
"github.com/eryajf/go-ldap-admin/model" "github.com/eryajf/go-ldap-admin/model"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
) )
// 全局mysql数据库变量 // 全局数据库对象
var DB *gorm.DB var DB *gorm.DB
// 初始化mysql数据库 // 初始化数据库
func InitMysql() { func InitDB() {
if config.Conf.Database.Driver == "mysql" {
initMysql()
} else if config.Conf.Database.Driver == "sqlite3" {
initSqlite3()
}
}
func initSqlite3() {
db, err := gorm.Open(sqlite.Open(config.Conf.Database.Source), &gorm.Config{
// 禁用外键(指定外键时不会在mysql创建真实的外键约束)
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
Log.Panicf("failed to connect sqlite3: %v", err)
}
DB = db
// 自动迁移表结构
dbAutoMigrate()
Log.Infof("初始化 sqlite3 数据库完成!")
}
func initMysql() {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&collation=%s&%s", dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&collation=%s&%s",
config.Conf.Mysql.Username, config.Conf.Mysql.Username,
config.Conf.Mysql.Password, config.Conf.Mysql.Password,
@ -40,10 +64,6 @@ func InitMysql() {
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
// 禁用外键(指定外键时不会在mysql创建真实的外键约束) // 禁用外键(指定外键时不会在mysql创建真实的外键约束)
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,
//// 指定表前缀
//NamingStrategy: schema.NamingStrategy{
// TablePrefix: config.Conf.Mysql.TablePrefix + "_",
//},
}) })
if err != nil { if err != nil {
Log.Panicf("初始化mysql数据库异常: %v", err) Log.Panicf("初始化mysql数据库异常: %v", err)
@ -70,6 +90,5 @@ func dbAutoMigrate() {
&model.Menu{}, &model.Menu{},
&model.Api{}, &model.Api{},
&model.OperationLog{}, &model.OperationLog{},
&model.FieldRelation{},
) )
} }

View File

@ -48,7 +48,7 @@ func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.Operation
// List 获取数据列表 // List 获取数据列表
func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.OperationLog, error) { func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.OperationLog, error) {
var list []*model.OperationLog var list []*model.OperationLog
db := common.DB.Model(&model.OperationLog{}).Order("start_time DESC") db := common.DB.Model(&model.OperationLog{}).Order("id DESC")
username := strings.TrimSpace(req.Username) username := strings.TrimSpace(req.Username)
if username != "" { if username != "" {

View File

@ -18,7 +18,7 @@ func InitConfig() {
common.InitLogger() common.InitLogger()
// 初始化数据库(mysql) // 初始化数据库(mysql)
common.InitMysql() common.InitDB()
// 初始化ldap连接 // 初始化ldap连接
common.InitLDAP() common.InitLDAP()