feat: 数据库存储系统支持sqlite3 (#141)
This commit is contained in:
parent
8959e4949f
commit
0b73508fd2
|
@ -12,6 +12,7 @@ build.sh
|
|||
logs
|
||||
go-web-mini
|
||||
go-ldap-admin
|
||||
go-ldap-admin.db
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
|
|
@ -27,6 +27,12 @@ logs:
|
|||
# 是否压缩
|
||||
compress: false
|
||||
|
||||
database:
|
||||
# 数据库类型 mysql sqlite3
|
||||
driver: mysql
|
||||
# 数据库连接sqlite3数据文件的路径
|
||||
source: go-ldap-admin.db
|
||||
|
||||
mysql:
|
||||
# 用户名
|
||||
username: root
|
||||
|
|
|
@ -18,6 +18,7 @@ var Conf = new(config)
|
|||
type config struct {
|
||||
System *SystemConfig `mapstructure:"system" json:"system"`
|
||||
Logs *LogsConfig `mapstructure:"logs" json:"logs"`
|
||||
Database *Database `mapstructure:"database" json:"database"`
|
||||
Mysql *MysqlConfig `mapstructure:"mysql" json:"mysql"`
|
||||
Casbin *CasbinConfig `mapstructure:"casbin" json:"casbin"`
|
||||
Jwt *JwtConfig `mapstructure:"jwt" json:"jwt"`
|
||||
|
@ -104,6 +105,11 @@ type LogsConfig struct {
|
|||
Compress bool `mapstructure:"compress" json:"compress"`
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Driver string `mapstructure:"driver" json:"driver"`
|
||||
Source string `mapstructure:"source" json:"source"`
|
||||
}
|
||||
|
||||
type MysqlConfig struct {
|
||||
Username string `mapstructure:"username" json:"username"`
|
||||
Password string `mapstructure:"password" json:"password"`
|
||||
|
|
6
go.mod
6
go.mod
|
@ -22,7 +22,8 @@ require (
|
|||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
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 (
|
||||
|
@ -37,6 +38,7 @@ require (
|
|||
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect
|
||||
github.com/golang/mock v1.6.0 // 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/reflect2 v1.0.2 // 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/pgx/v4 v4.15.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/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/magiconair/properties v1.8.6 // indirect
|
||||
|
|
11
go.sum
11
go.sum
|
@ -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/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.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
|
||||
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.9/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.3.1 h1:Pyv+gg1Gq1IgsLYytj/S2k7ebII3CzEdpqQkPOdH24g=
|
||||
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.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.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI=
|
||||
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.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.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0=
|
||||
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||
gorm.io/gorm v1.23.4/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-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
|
2
main.go
2
main.go
|
@ -26,7 +26,7 @@ func main() {
|
|||
common.InitLogger()
|
||||
|
||||
// 初始化数据库(mysql)
|
||||
common.InitMysql()
|
||||
common.InitDB()
|
||||
|
||||
// 初始化ldap连接
|
||||
common.InitLDAP()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -56,9 +57,9 @@ func OperationLogMiddleware() gin.HandlerFunc {
|
|||
Path: path,
|
||||
Remark: api.Remark,
|
||||
Status: c.Writer.Status(),
|
||||
StartTime: startTime,
|
||||
StartTime: fmt.Sprintf("%v", startTime),
|
||||
TimeCost: timeCost,
|
||||
//UserAgent: c.Request.UserAgent(),
|
||||
UserAgent: c.Request.UserAgent(),
|
||||
}
|
||||
|
||||
// 最好是将日志发送到rabbitmq或者kafka中
|
||||
|
|
|
@ -12,7 +12,7 @@ type Menu struct {
|
|||
Path string `gorm:"type:varchar(100);comment:'菜单访问路径'" json:"path"`
|
||||
Redirect string `gorm:"type:varchar(100);comment:'重定向路径'" json:"redirect"`
|
||||
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"`
|
||||
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"`
|
||||
|
|
|
@ -1,21 +1,19 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type OperationLog struct {
|
||||
gorm.Model
|
||||
Username string `gorm:"type:varchar(20);comment:'用户登录名'" json:"username"`
|
||||
Ip string `gorm:"type:varchar(20);comment:'Ip地址'" json:"ip"`
|
||||
IpLocation string `gorm:"type:varchar(20);comment:'Ip所在地'" json:"ipLocation"`
|
||||
Method string `gorm:"type:varchar(20);comment:'请求方式'" json:"method"`
|
||||
Path string `gorm:"type:varchar(100);comment:'访问路径'" json:"path"`
|
||||
Remark string `gorm:"type:varchar(100);comment:'备注'" json:"remark"`
|
||||
Status int `gorm:"type:int(4);comment:'响应状态码'" json:"status"`
|
||||
StartTime time.Time `gorm:"type:datetime(3);comment:'发起时间'" json:"startTime"`
|
||||
TimeCost int64 `gorm:"type:int(6);comment:'请求耗时(ms)'" json:"timeCost"`
|
||||
UserAgent string `gorm:"type:varchar(20);comment:'浏览器标识'" json:"userAgent"`
|
||||
Username string `gorm:"type:varchar(20);comment:'用户登录名'" json:"username"`
|
||||
Ip string `gorm:"type:varchar(20);comment:'Ip地址'" json:"ip"`
|
||||
IpLocation string `gorm:"type:varchar(20);comment:'Ip所在地'" json:"ipLocation"`
|
||||
Method string `gorm:"type:varchar(20);comment:'请求方式'" json:"method"`
|
||||
Path string `gorm:"type:varchar(100);comment:'访问路径'" json:"path"`
|
||||
Remark string `gorm:"type:varchar(100);comment:'备注'" json:"remark"`
|
||||
Status int `gorm:"type:int(4);comment:'响应状态码'" json:"status"`
|
||||
StartTime string `gorm:"type:varchar(2048);comment:'发起时间'" json:"startTime"`
|
||||
TimeCost int64 `gorm:"type:int(6);comment:'请求耗时(ms)'" json:"timeCost"`
|
||||
UserAgent string `gorm:"type:varchar(2048);comment:'浏览器标识'" json:"userAgent"`
|
||||
}
|
||||
|
|
|
@ -7,14 +7,38 @@ import (
|
|||
"github.com/eryajf/go-ldap-admin/model"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 全局mysql数据库变量
|
||||
// 全局数据库对象
|
||||
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",
|
||||
config.Conf.Mysql.Username,
|
||||
config.Conf.Mysql.Password,
|
||||
|
@ -40,10 +64,6 @@ func InitMysql() {
|
|||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
// 禁用外键(指定外键时不会在mysql创建真实的外键约束)
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
//// 指定表前缀
|
||||
//NamingStrategy: schema.NamingStrategy{
|
||||
// TablePrefix: config.Conf.Mysql.TablePrefix + "_",
|
||||
//},
|
||||
})
|
||||
if err != nil {
|
||||
Log.Panicf("初始化mysql数据库异常: %v", err)
|
||||
|
@ -70,6 +90,5 @@ func dbAutoMigrate() {
|
|||
&model.Menu{},
|
||||
&model.Api{},
|
||||
&model.OperationLog{},
|
||||
&model.FieldRelation{},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.Operation
|
|||
// List 获取数据列表
|
||||
func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.OperationLog, error) {
|
||||
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)
|
||||
if username != "" {
|
||||
|
|
|
@ -18,7 +18,7 @@ func InitConfig() {
|
|||
common.InitLogger()
|
||||
|
||||
// 初始化数据库(mysql)
|
||||
common.InitMysql()
|
||||
common.InitDB()
|
||||
|
||||
// 初始化ldap连接
|
||||
common.InitLDAP()
|
||||
|
|
Loading…
Reference in New Issue