Skip to content

Commit

Permalink
add config file recover function
Browse files Browse the repository at this point in the history
  • Loading branch information
snowlyg committed Oct 2, 2022
1 parent 5c7fe35 commit 64673c4
Show file tree
Hide file tree
Showing 23 changed files with 156 additions and 102 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ example/iris/tmp
example/iris/www
example/iris/config
.idea/
.DS_Store

14 changes: 14 additions & 0 deletions g/var.go
Original file line number Diff line number Diff line change
@@ -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"))
)
15 changes: 7 additions & 8 deletions migration/migrate_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions server/cache/cmd_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 8 additions & 2 deletions server/cache/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cache

import (
"encoding/json"
"fmt"
"strconv"

Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions server/cache/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 4 additions & 7 deletions server/cache/index_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions server/casbin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()

Expand Down
18 changes: 14 additions & 4 deletions server/database/config.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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
},
//
Expand Down
6 changes: 3 additions & 3 deletions server/database/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()

Expand Down
20 changes: 13 additions & 7 deletions server/mongodb/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mongodb

import (
"encoding/json"
"fmt"
"time"

Expand All @@ -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,
Expand All @@ -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
Expand Down
Loading

0 comments on commit 64673c4

Please sign in to comment.