diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 006b9fdc..00000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore index 194a7675..12e41212 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ example/iris/tmp example/iris/www example/iris/config .idea/ +.DS_Store diff --git a/g/var.go b/g/var.go new file mode 100644 index 00000000..802bf933 --- /dev/null +++ b/g/var.go @@ -0,0 +1,14 @@ +package g + +import ( + "os" + "strings" +) + +var ( + TestRedisPwd = strings.TrimSpace(os.Getenv("redisPwd")) + TestMysqlAddr = strings.TrimSpace(os.Getenv("mysqlAddr")) + TestMysqlPwd = strings.TrimSpace(os.Getenv("mysqlPwd")) + TestMongoAddr = strings.TrimSpace(os.Getenv("mongoAddr")) + TestDbType = strings.TrimSpace(os.Getenv("dbType")) +) diff --git a/migration/migrate_test.go b/migration/migrate_test.go index f1156fad..23ec933e 100644 --- a/migration/migrate_test.go +++ b/migration/migrate_test.go @@ -1,11 +1,10 @@ package migration import ( - "os" - "strings" "testing" "github.com/go-gormigrate/gormigrate/v2" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/database" "github.com/snowlyg/iris-admin/server/zap_server" "gorm.io/gorm" @@ -55,8 +54,8 @@ func TestAddSeed(t *testing.T) { func TestMigrate(t *testing.T) { defer zap_server.Remove() defer database.Remove() - database.CONFIG.Path = strings.TrimSpace(os.Getenv("mysqlAddr")) - database.CONFIG.Password = os.Getenv("mysqlPwd") + database.CONFIG.Path = g.TestMysqlAddr + database.CONFIG.Password = g.TestMysqlPwd migrate := New() migrate.AddMigration(m) t.Run("migrate migrate", func(t *testing.T) { @@ -69,8 +68,8 @@ func TestMigrate(t *testing.T) { func TestRollback(t *testing.T) { defer zap_server.Remove() defer database.Remove() - database.CONFIG.Path = strings.TrimSpace(os.Getenv("mysqlAddr")) - database.CONFIG.Password = os.Getenv("mysqlPwd") + database.CONFIG.Path = g.TestMysqlAddr + database.CONFIG.Password = g.TestMysqlPwd migrate := New() t.Run("migrate rollback no migrate with id", func(t *testing.T) { err := migrate.Rollback(id) @@ -107,8 +106,8 @@ func TestRollback(t *testing.T) { func TestRefresh(t *testing.T) { defer zap_server.Remove() defer database.Remove() - database.CONFIG.Path = strings.TrimSpace(os.Getenv("mysqlAddr")) - database.CONFIG.Password = os.Getenv("mysqlPwd") + database.CONFIG.Path = g.TestMysqlAddr + database.CONFIG.Password = g.TestMysqlPwd migrate := New() t.Run("migrate refresh no migrate", func(t *testing.T) { diff --git a/server/cache/cmd_test.go b/server/cache/cmd_test.go index e8755ac1..5970cf11 100644 --- a/server/cache/cmd_test.go +++ b/server/cache/cmd_test.go @@ -1,20 +1,24 @@ package cache import ( - "os" "testing" + + "github.com/snowlyg/iris-admin/g" + "github.com/snowlyg/iris-admin/server/zap_server" ) func TestInitConfig(t *testing.T) { + defer zap_server.Remove() t.Run("test redis's file initialize", func(t *testing.T) { - redisPwd := os.Getenv("redisPwd") - CONFIG.Password = redisPwd + CONFIG.Password = g.TestRedisPwd err := InitConfig() if err != nil { t.Error(err) + return } if !IsExist() { t.Errorf("config's files is not exist.") + return } if err := Remove(); err != nil { t.Error(err) diff --git a/server/cache/config.go b/server/cache/config.go index ac22ce92..18548006 100644 --- a/server/cache/config.go +++ b/server/cache/config.go @@ -1,6 +1,7 @@ package cache import ( + "encoding/json" "fmt" "strconv" @@ -30,11 +31,16 @@ func IsExist() bool { // Remove remove config file func Remove() error { - err := getViperConfig().Remove() + return getViperConfig().Remove() +} + +// Recover +func Recover() error { + b, err := json.Marshal(CONFIG) if err != nil { return err } - return nil + return getViperConfig().Recover(b) } // getViperConfig get viper config diff --git a/server/cache/config_test.go b/server/cache/config_test.go index c78fca8d..03e8279b 100644 --- a/server/cache/config_test.go +++ b/server/cache/config_test.go @@ -1,21 +1,10 @@ package cache import ( - "os" "testing" - - "github.com/snowlyg/iris-admin/server/viper_server" ) func TestIsExist(t *testing.T) { - viper_server.Init(getViperConfig()) - t.Run("test redis's IsExist function", func(t *testing.T) { - redisPwd := os.Getenv("redisPwd") - CONFIG.Password = redisPwd - if !IsExist() { - t.Errorf("config's files is not exist.") - } - }) t.Run("Test Remove function", func(t *testing.T) { if err := Remove(); err != nil { t.Error(err) diff --git a/server/cache/index_test.go b/server/cache/index_test.go index f1d41d9b..deb70826 100644 --- a/server/cache/index_test.go +++ b/server/cache/index_test.go @@ -1,19 +1,18 @@ package cache import ( - "os" "reflect" "testing" "time" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/zap_server" ) func TestSetCacheString(t *testing.T) { defer Remove() defer zap_server.Remove() - redisPwd := os.Getenv("redisPwd") - CONFIG.Password = redisPwd + CONFIG.Password = g.TestRedisPwd t.Run("test set cache string", func(t *testing.T) { key := "test_set_cache" want := "test_set_cache_value" @@ -39,8 +38,7 @@ func TestSetCacheString(t *testing.T) { func TestSetCacheUint(t *testing.T) { defer Remove() defer zap_server.Remove() - redisPwd := os.Getenv("redisPwd") - CONFIG.Password = redisPwd + CONFIG.Password = g.TestRedisPwd t.Run("test set cache uint", func(t *testing.T) { key := "test_set_cache" var want uint64 = 123 @@ -65,8 +63,7 @@ func TestSetCacheUint(t *testing.T) { func TestSetCacheBytes(t *testing.T) { defer Remove() defer zap_server.Remove() - redisPwd := os.Getenv("redisPwd") - CONFIG.Password = redisPwd + CONFIG.Password = g.TestRedisPwd t.Run("test set cache bytes", func(t *testing.T) { key := "test_set_cache" want := []byte("test_set_cache_value") diff --git a/server/casbin/main_test.go b/server/casbin/main_test.go index 8c341ec9..7fe3d323 100644 --- a/server/casbin/main_test.go +++ b/server/casbin/main_test.go @@ -3,11 +3,11 @@ package casbin import ( _ "embed" "os" - "strings" "testing" "github.com/bwmarrin/snowflake" "github.com/snowlyg/helper/str" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/database" "github.com/snowlyg/iris-admin/server/zap_server" ) @@ -18,8 +18,8 @@ func TestMain(m *testing.M) { uuid := str.Join("casbin", "_", node.Generate().String()) database.CONFIG.Dbname = uuid - database.CONFIG.Path = strings.TrimSpace(os.Getenv("mysqlAddr")) - database.CONFIG.Password = strings.TrimSpace(os.Getenv("mysqlPwd")) + database.CONFIG.Path = g.TestMysqlAddr + database.CONFIG.Password = g.TestMysqlPwd Instance() diff --git a/server/database/config.go b/server/database/config.go index 77a4d0d7..b0944963 100644 --- a/server/database/config.go +++ b/server/database/config.go @@ -1,8 +1,10 @@ package database import ( + "encoding/json" "fmt" + "github.com/fsnotify/fsnotify" "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/viper_server" "github.com/spf13/viper" @@ -37,7 +39,7 @@ func (m *Mysql) Dsn() string { return fmt.Sprintf("%s%s?%s", m.BaseDsn(), m.Dbname, m.Config) } -// Dsn return +// Dsn return func (m *Mysql) BaseDsn() string { return fmt.Sprintf("%s:%s@tcp(%s)/", m.Username, m.Password, m.Path) } @@ -49,11 +51,16 @@ func IsExist() bool { // Remove remove config file func Remove() error { - err := getViperConfig().Remove() + return getViperConfig().Remove() +} + +// Recover +func Recover() error { + b, err := json.Marshal(CONFIG) if err != nil { return err } - return nil + return getViperConfig().Recover(b) } // getViperConfig get viper config @@ -72,7 +79,10 @@ func getViperConfig() viper_server.ViperConfig { return fmt.Errorf("get Unarshal error: %v", err) } // watch config file change - vi.SetConfigName(configName) + vi.OnConfigChange(func(e fsnotify.Event) { + fmt.Println("Config file changed:", e.Name) + }) + vi.WatchConfig() return nil }, // diff --git a/server/database/main_test.go b/server/database/main_test.go index 5a1388b3..eeaa949c 100644 --- a/server/database/main_test.go +++ b/server/database/main_test.go @@ -3,11 +3,11 @@ package database import ( _ "embed" "os" - "strings" "testing" "github.com/bwmarrin/snowflake" "github.com/snowlyg/helper/str" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/zap_server" ) @@ -17,8 +17,8 @@ func TestMain(m *testing.M) { uuid := str.Join("database", "_", node.Generate().String()) CONFIG.Dbname = uuid - CONFIG.Path = strings.TrimSpace(os.Getenv("mysqlAddr")) - CONFIG.Password = strings.TrimSpace(os.Getenv("mysqlPwd")) + CONFIG.Path = g.TestMysqlAddr + CONFIG.Password = g.TestMysqlPwd Instance() diff --git a/server/mongodb/config.go b/server/mongodb/config.go index a04a0755..7df12a77 100644 --- a/server/mongodb/config.go +++ b/server/mongodb/config.go @@ -1,6 +1,7 @@ package mongodb import ( + "encoding/json" "fmt" "time" @@ -10,6 +11,11 @@ import ( "github.com/spf13/viper" ) +// init initialize +func init() { + viper_server.Init(getViperConfig()) +} + var CONFIG = MongoDB{ DB: "mongo_test", Timeout: 10, @@ -33,16 +39,16 @@ func IsExist() bool { // Remove remove config file func Remove() error { - err := getViperConfig().Remove() + return getViperConfig().Remove() +} + +// Recover +func Recover() error { + b, err := json.Marshal(CONFIG) if err != nil { return err } - return nil -} - -// init initialize -func init() { - viper_server.Init(getViperConfig()) + return getViperConfig().Recover(b) } // getViperConfig get viper config diff --git a/server/mongodb/index_test.go b/server/mongodb/index_test.go index 24797108..5ccc3765 100644 --- a/server/mongodb/index_test.go +++ b/server/mongodb/index_test.go @@ -2,10 +2,10 @@ package mongodb import ( "context" - "os" "testing" "time" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/cache" _ "github.com/snowlyg/iris-admin/server/cache" "go.mongodb.org/mongo-driver/bson" @@ -13,7 +13,7 @@ import ( ) func TestGetClient(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -36,7 +36,7 @@ func TestGetClient(t *testing.T) { } func TestPing(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -63,7 +63,7 @@ func TestPing(t *testing.T) { }) } func TestInsertOne(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -95,7 +95,7 @@ func TestInsertOne(t *testing.T) { }) } func TestGetCollection(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -122,7 +122,7 @@ func TestGetCollection(t *testing.T) { } func TestGetAggregate(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -166,7 +166,7 @@ func TestGetAggregate(t *testing.T) { } func TestFind(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -198,7 +198,7 @@ func TestFind(t *testing.T) { } func TestFindOne(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -226,7 +226,7 @@ func TestFindOne(t *testing.T) { } func TestDeleteOne(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) @@ -255,7 +255,7 @@ func TestDeleteOne(t *testing.T) { } func TestUpdateOne(t *testing.T) { - CONFIG.Addr = os.Getenv("mongoAddr") + CONFIG.Addr = g.TestMongoAddr defer Remove() defer cache.Remove() ctx, cancel := context.WithTimeout(context.Background(), CONFIG.Timeout*time.Second) diff --git a/server/operation/config.go b/server/operation/config.go index 0f7984af..2eb0fd8a 100644 --- a/server/operation/config.go +++ b/server/operation/config.go @@ -1,6 +1,7 @@ package operation import ( + "encoding/json" "fmt" "strings" @@ -27,11 +28,16 @@ func IsExist() bool { // Remove remove config file func Remove() error { - err := getViperConfig().Remove() + return getViperConfig().Remove() +} + +// Recover +func Recover() error { + b, err := json.Marshal(CONFIG) if err != nil { return err } - return nil + return getViperConfig().Recover(b) } // Operation diff --git a/server/operation/main_test.go b/server/operation/main_test.go index 797c9838..9faf1d09 100644 --- a/server/operation/main_test.go +++ b/server/operation/main_test.go @@ -3,11 +3,11 @@ package operation import ( _ "embed" "os" - "strings" "testing" "github.com/bwmarrin/snowflake" "github.com/snowlyg/helper/str" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/server/database" "github.com/snowlyg/iris-admin/server/zap_server" ) @@ -18,8 +18,8 @@ func TestMain(m *testing.M) { uuid := str.Join("operation", "_", node.Generate().String()) database.CONFIG.Dbname = uuid - database.CONFIG.Path = strings.TrimSpace(os.Getenv("mysqlAddr")) - database.CONFIG.Password = strings.TrimSpace(os.Getenv("mysqlPwd")) + database.CONFIG.Path = g.TestMysqlAddr + database.CONFIG.Password = g.TestMysqlPwd database.Instance() code := m.Run() diff --git a/server/viper_server/index.go b/server/viper_server/index.go index 04078a53..ed60ab03 100644 --- a/server/viper_server/index.go +++ b/server/viper_server/index.go @@ -47,6 +47,12 @@ func (vc ViperConfig) Remove() error { return dir.Remove(vc.getConfigFilePath()) } +// Recover recover config file content +func (vc ViperConfig) Recover(b []byte) error { + _, err := dir.WriteBytes(vc.getConfigFilePath(), b) + return err +} + // Init func Init(vc ViperConfig) error { @@ -69,6 +75,7 @@ func Init(vc ViperConfig) error { if vc.Debug { fmt.Printf("this config file's type is [%s]\n", vc.Type) } + vi.SetConfigName(vc.Name) vi.SetConfigType(vc.Type) vi.AddConfigPath(vc.Directory) diff --git a/server/web/cmd.go b/server/web/cmd.go index 41c26ba0..606a1ac1 100644 --- a/server/web/cmd.go +++ b/server/web/cmd.go @@ -5,10 +5,11 @@ import ( "strings" "github.com/snowlyg/iris-admin/server/database" + "github.com/snowlyg/iris-admin/server/viper_server" ) -// Init initialize -func Init() error { +// Initialize initialize +func Initialize() error { var cover string if IsExist() { fmt.Println("Your web config is initialized , reinitialized web will cover your web config.") @@ -67,7 +68,7 @@ func initConfig() error { if systemAddr != "" { CONFIG.System.Addr = systemAddr } - err := InitWeb() + err := viper_server.Init(getViperConfig()) if err != nil { return err } diff --git a/server/web/common/index.go b/server/web/common/index.go index 6171b4b7..be380a52 100644 --- a/server/web/common/index.go +++ b/server/web/common/index.go @@ -2,11 +2,10 @@ package common import ( "fmt" - "os" - "strings" "github.com/bwmarrin/snowflake" "github.com/snowlyg/helper/str" + "github.com/snowlyg/iris-admin/g" "github.com/snowlyg/iris-admin/migration" "github.com/snowlyg/iris-admin/server/casbin" "github.com/snowlyg/iris-admin/server/database" @@ -21,30 +20,35 @@ import ( // BeforeTestMainGin func BeforeTestMainGin(party func(wi *web_gin.WebServer), seed func(wi *web_gin.WebServer, mc *migration.MigrationCmd)) (string, *web_gin.WebServer) { fmt.Println("+++++ TEST BEGAIN +++++") + zap_server.CONFIG.LogInConsole = true + zap_server.Recover() - dbType := os.Getenv("dbType") + dbType := g.TestDbType if dbType != "" { web.CONFIG.System.DbType = dbType } - web.InitWeb() - - zap_server.CONFIG.LogInConsole = true + web.Recover() node, _ := snowflake.NewNode(1) uuid := str.Join("gin", "_", node.Generate().String()) database.CONFIG.Dbname = uuid - mysqlPwd := os.Getenv("mysqlPwd") + mysqlPwd := g.TestMysqlPwd if mysqlPwd != "" { - database.CONFIG.Password = strings.TrimSpace(mysqlPwd) + database.CONFIG.Password = mysqlPwd } - mysqlAddr := os.Getenv("mysqlAddr") + mysqlAddr := g.TestMysqlAddr if mysqlAddr != "" { - database.CONFIG.Path = strings.TrimSpace(mysqlAddr) + database.CONFIG.Path = mysqlAddr } database.CONFIG.LogMode = true - // database.InitMysql() + database.Recover() + + if database.Instance() == nil { + fmt.Println("database instance is nil") + return uuid, nil + } wi := web_gin.Init() party(wi) @@ -70,30 +74,35 @@ func BeforeTestMainGin(party func(wi *web_gin.WebServer), seed func(wi *web_gin. // BeforeTestMainIris func BeforeTestMainIris(party func(wi *web_iris.WebServer), seed func(wi *web_iris.WebServer, mc *migration.MigrationCmd)) (string, *web_iris.WebServer) { fmt.Println("+++++ TEST BEGAIN +++++") + zap_server.CONFIG.LogInConsole = true + zap_server.Recover() - dbType := os.Getenv("dbType") + dbType := g.TestDbType if dbType != "" { web.CONFIG.System.DbType = dbType } - web.InitWeb() - - zap_server.CONFIG.LogInConsole = true + web.Recover() node, _ := snowflake.NewNode(1) uuid := str.Join("iris", "_", node.Generate().String()) database.CONFIG.Dbname = uuid - mysqlPwd := os.Getenv("mysqlPwd") + mysqlPwd := g.TestMysqlPwd if mysqlPwd != "" { - database.CONFIG.Password = strings.TrimSpace(mysqlPwd) + database.CONFIG.Password = mysqlPwd } - mysqlAddr := os.Getenv("mysqlAddr") + mysqlAddr := g.TestMysqlAddr if mysqlAddr != "" { - database.CONFIG.Path = strings.TrimSpace(mysqlAddr) + database.CONFIG.Path = mysqlAddr } database.CONFIG.LogMode = true - // database.InitMysql() + database.Recover() + + if database.Instance() == nil { + fmt.Println("database instance is nil") + return uuid, nil + } wi := web_iris.Init() party(wi) diff --git a/server/web/config.go b/server/web/config.go index 1dde86a9..bf95d443 100644 --- a/server/web/config.go +++ b/server/web/config.go @@ -1,6 +1,7 @@ package web import ( + "encoding/json" "fmt" "path/filepath" "strconv" @@ -97,11 +98,16 @@ func IsExist() bool { // Remove remove config file func Remove() error { - err := getViperConfig().Remove() + return getViperConfig().Remove() +} + +// Recover +func Recover() error { + b, err := json.Marshal(CONFIG) if err != nil { return err } - return nil + return getViperConfig().Recover(b) } // getViperConfig get viper config diff --git a/server/web/index.go b/server/web/index.go index 08659430..73687bb6 100644 --- a/server/web/index.go +++ b/server/web/index.go @@ -5,6 +5,11 @@ import ( "github.com/snowlyg/iris-admin/server/zap_server" ) +// init +func init() { + viper_server.Init(getViperConfig()) +} + type WebBaseFunc interface { AddWebStatic(staticAbsPath, webPrefix string, paths ...string) AddUploadStatic(staticAbsPath, webPrefix string) @@ -39,13 +44,3 @@ func StartTest(wf WebFunc) { zap_server.ZAPLOG.Error(err.Error()) } } - -// InitWeb -func InitWeb() error { - err := viper_server.Init(getViperConfig()) - if err != nil { - zap_server.ZAPLOG.Error(err.Error()) - return err - } - return nil -} diff --git a/server/web/web_gin/index.go b/server/web/web_gin/index.go index d654f0c0..85c284de 100644 --- a/server/web/web_gin/index.go +++ b/server/web/web_gin/index.go @@ -40,7 +40,6 @@ type WebStatic struct { // Init func Init() *WebServer { - web.InitWeb() gin.SetMode(web.CONFIG.System.Level) app := gin.Default() if web.CONFIG.System.Tls { diff --git a/server/web/web_iris/index.go b/server/web/web_iris/index.go index 50d8fb16..de0b29e4 100644 --- a/server/web/web_iris/index.go +++ b/server/web/web_iris/index.go @@ -41,7 +41,6 @@ type Party struct { // Init func Init() *WebServer { - web.InitWeb() app := iris.New() if web.CONFIG.System.Tls { app.Use(middleware.LoadTls()) diff --git a/server/zap_server/config.go b/server/zap_server/config.go index 2d8666c6..88655f9a 100644 --- a/server/zap_server/config.go +++ b/server/zap_server/config.go @@ -1,6 +1,7 @@ package zap_server import ( + "encoding/json" "fmt" "strconv" @@ -40,11 +41,16 @@ func IsExist() bool { // Remove remove config file func Remove() error { - err := getViperConfig().Remove() + return getViperConfig().Remove() +} + +// Recover +func Recover() error { + b, err := json.Marshal(CONFIG) if err != nil { return err } - return nil + return getViperConfig().Recover(b) } // getViperConfig get viper config