From f4bd197df0ab3cc0c0ab27c101629aeb532e0563 Mon Sep 17 00:00:00 2001 From: eryajf Date: Tue, 26 Nov 2024 08:50:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0sqlite=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8,=E4=BE=BF=E4=BA=8E=E5=BF=AB=E9=80=9F=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + config/config.go | 25 ++++++++++------- config/config.yaml | 8 +++++- go.mod | 8 ++++++ go.sum | 21 ++++++++++++-- pkg/client/db.go | 69 +++++++++++++++++++++++++++++++++------------- 6 files changed, 100 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 848f41e..1562cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ lerna-debug.log* node_modules dist dist-ssr +data *.local # Editor directories and files diff --git a/config/config.go b/config/config.go index 65eb481..ed9a0bc 100644 --- a/config/config.go +++ b/config/config.go @@ -1,17 +1,19 @@ package config import ( - "github.com/spf13/viper" "log" + + "github.com/spf13/viper" ) type App struct { - Server Server `json:"Server"` - MySQL MySQL `json:"MySQL"` - Redis Redis `json:"Redis"` - Jwt Jwt `json:"Jwt"` - Jaeger Jaeger `json:"Jaeger"` - Ldap Ldap `json:"ldap"` + Server Server `json:"Server"` + DataBase DataBase `json:"DataBase"` + MySQL MySQL `json:"MySQL"` + Redis Redis `json:"Redis"` + Jwt Jwt `json:"Jwt"` + Jaeger Jaeger `json:"Jaeger"` + Ldap Ldap `json:"ldap"` } type Server struct { @@ -26,6 +28,11 @@ type AlarmConfig struct { RecoverWait int `json:"recoverWait"` } +type DataBase struct { + Driver string `json:"driver"` + Source string `json:"source"` +} + type MySQL struct { Host string `json:"host"` Port string `json:"port"` @@ -61,9 +68,7 @@ type Ldap struct { Cronjob string `json:"cronjob"` } -var ( - configFile = "config/config.yaml" -) +var configFile = "config/config.yaml" func InitConfig() App { v := viper.New() diff --git a/config/config.yaml b/config/config.yaml index a2ba07c..631ebc3 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,7 +1,13 @@ Server: port: "9001" # release / debug / test - mode: "release" + mode: "debug" + +DataBase: + # 数据库类型 mysql sqlite3 + driver: sqlite3 + # 数据库连接sqlite3数据文件的路径 + source: data/watchalert.db MySQL: host: 127.0.0.1 diff --git a/go.mod b/go.mod index 1eddcd3..bbfdc4b 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/rds v1.79.5 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gin-gonic/gin v1.9.1 + github.com/glebarez/sqlite v1.11.0 github.com/go-ping/ping v1.1.0 github.com/go-redis/redis v6.15.9+incompatible github.com/google/uuid v1.6.0 @@ -66,11 +67,13 @@ require ( github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/clbanning/mxj/v2 v2.5.5 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/fatih/color v1.17.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect @@ -108,6 +111,7 @@ require ( github.com/pierrec/lz4 v2.6.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect @@ -136,6 +140,10 @@ require ( k8s.io/klog/v2 v2.110.1 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect + modernc.org/libc v1.22.5 // indirect + modernc.org/mathutil v1.5.0 // indirect + modernc.org/memory v1.5.0 // indirect + modernc.org/sqlite v1.23.1 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.3.0 // indirect diff --git a/go.sum b/go.sum index 97898f0..7b2a84a 100644 --- a/go.sum +++ b/go.sum @@ -102,6 +102,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -124,6 +126,10 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= +github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= +github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -183,8 +189,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= -github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -286,6 +292,9 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= @@ -533,6 +542,14 @@ k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/A k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= +modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY= +modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= +modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= +modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= +modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= +modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= diff --git a/pkg/client/db.go b/pkg/client/db.go index e14b019..51f19ac 100644 --- a/pkg/client/db.go +++ b/pkg/client/db.go @@ -3,36 +3,36 @@ package client import ( "context" "fmt" + + "watchAlert/internal/global" + "watchAlert/internal/models" + + "github.com/glebarez/sqlite" "github.com/zeromicro/go-zero/core/logc" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" - "watchAlert/internal/global" - "watchAlert/internal/models" ) -func InitDB() *gorm.DB { +// 全局数据库对象 +var db *gorm.DB +func InitDB() *gorm.DB { // 初始化本地 test.db 数据库文件 - //db, err := gorm.Open(sqlite.Open("data/sql.db"), &gorm.Config{}) - - sql := global.Config.MySQL - dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4,utf8&parseTime=True&loc=Local&timeout=%s", - sql.User, - sql.Pass, - sql.Host, - sql.Port, - sql.DBName, - sql.Timeout) - db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) - - if err != nil { - logc.Errorf(context.Background(), "failed to connect database: %s", err.Error()) - return nil + // db, err := gorm.Open(sqlite.Open("data/sql.db"), &gorm.Config{}) + switch global.Config.DataBase.Driver { + case "mysql": + db = ConnMysql() + case "sqlite3": + db = ConnSqlite() } + dbAutoMigrate() + return db +} +func dbAutoMigrate() { // 检查 Product 结构是否变化,变化则进行迁移 - err = db.AutoMigrate( + _ = db.AutoMigrate( &models.DutySchedule{}, &models.DutyManagement{}, &models.AlertNotice{}, @@ -59,10 +59,41 @@ func InitDB() *gorm.DB { &models.NoticeRecord{}, &models.ProbingRule{}, ) +} + +func ConnSqlite() *gorm.DB { + db, err := gorm.Open(sqlite.Open(global.Config.DataBase.Source), &gorm.Config{ + // 禁用外键(指定外键时不会在mysql创建真实的外键约束) + DisableForeignKeyConstraintWhenMigrating: true, + }) + if err != nil { + logc.Errorf(context.Background(), "failed to connect database: %s", err.Error()) + return nil + } + dbObj, err := db.DB() if err != nil { logc.Error(context.Background(), err.Error()) return nil } + // 参见: https://github.com/glebarez/sqlite/issues/52 + dbObj.SetMaxOpenConns(1) + return db +} + +func ConnMysql() *gorm.DB { + sql := global.Config.MySQL + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4,utf8&parseTime=True&loc=Local&timeout=%s", + sql.User, + sql.Pass, + sql.Host, + sql.Port, + sql.DBName, + sql.Timeout) + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + logc.Errorf(context.Background(), "failed to connect database: %s", err.Error()) + return nil + } if global.Config.Server.Mode == "debug" { db.Debug()