Skip to content

Commit

Permalink
补充默认配置
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Dec 23, 2023
1 parent 65b6447 commit 1576a17
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 15 deletions.
19 changes: 15 additions & 4 deletions ioc/config/datasource/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/BurntSushi/toml"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/datasource"
)
Expand All @@ -17,7 +18,8 @@ func TestGetDB(t *testing.T) {
m := datasource.DB(ctx)
t.Log(m)

tx := m.Begin().WithContext(ctx)
//
tx := datasource.BeginTransaction(ctx)
defer datasource.EndTransaction(tx, nil)

// tx 业务处理
Expand All @@ -29,12 +31,21 @@ func TestGetDB(t *testing.T) {
t.Log(txCtx)
}

func TestDefaultConfig(t *testing.T) {
f, err := os.OpenFile("test/default.toml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
t.Fatal(err)
}
appConf := map[string]any{datasource.AppName: ioc.Config().Get(datasource.AppName)}
toml.NewEncoder(f).Encode(appConf)
}

func init() {
os.Setenv("DATASOURCE_HOST", "127.0.0.1")
os.Setenv("DATASOURCE_PORT", "3306")
os.Setenv("DATASOURCE_DB", "xxx")
os.Setenv("DATASOURCE_USERNAME", "xxx")
os.Setenv("DATASOURCE_PASSWORD", "xxx")
os.Setenv("DATASOURCE_DB", "test")
os.Setenv("DATASOURCE_USERNAME", "root")
os.Setenv("DATASOURCE_PASSWORD", "123456")
err := ioc.ConfigIocObject(ioc.NewLoadConfigRequest())
if err != nil {
panic(err)
Expand Down
4 changes: 1 addition & 3 deletions ioc/config/datasource/grom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ func init() {
Provider: PROVIDER_MYSQL,
Host: "127.0.0.1",
Port: 3306,
DB: "mcube",
Username: "root",
Password: "123456",
DB: application.App().Name(),
Debug: false,
EnableTrace: true,
})
Expand Down
9 changes: 9 additions & 0 deletions ioc/config/datasource/test/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[datasource]
provider = "mysql"
host = "127.0.0.1"
port = 3306
database = "test"
username = "root"
password = "123456"
debug = false
enable_trace = true
6 changes: 6 additions & 0 deletions ioc/config/datasource/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ func WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context {
return context.WithValue(ctx, TransactionCtxKey{}, tx)
}

// 开启事务
func BeginTransaction(ctx context.Context) *gorm.DB {
return DB(ctx).Begin().WithContext(ctx)
}

// 结束事务
func EndTransaction(tx *gorm.DB, err error) error {
if err != nil {
return tx.Rollback().Error
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Config struct {
// 日志的级别, 默认Debug
Level zerolog.Level `toml:"level" json:"level" yaml:"level" env:"LOG_LEVEL"`
// 开启Trace时, 记录的TraceId名称, 默认trace_id
TraceFiled string `toml:"TraceFiled" json:"TraceFiled" yaml:"TraceFiled" env:"LOG_TRACE_FILED"`
TraceFiled string `toml:"trace_filed" json:"trace_filed" yaml:"trace_filed" env:"LOG_TRACE_FILED"`

// 控制台日志配置
Console Console `toml:"console" json:"console" yaml:"console" envPrefix:"LOG_CONSOLE_"`
Expand Down
14 changes: 10 additions & 4 deletions ioc/config/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime/debug"
"testing"

"github.com/BurntSushi/toml"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/logger"
)
Expand All @@ -16,6 +17,15 @@ func TestGetClientGetter(t *testing.T) {
sub.Debug().Msgf("hello %s", "a")
}

func TestDefaultConfig(t *testing.T) {
f, err := os.OpenFile("test/default.toml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
t.Fatal(err)
}
appConf := map[string]any{logger.AppName: ioc.Config().Get(logger.AppName).(*logger.Config)}
toml.NewEncoder(f).Encode(appConf)
}

func TestPanicStack(t *testing.T) {
// 捕获 panic
defer func() {
Expand All @@ -29,10 +39,6 @@ func TestPanicStack(t *testing.T) {
}

func init() {
os.Setenv("LOG_CONSOLE_NO_COLOR", "true")
os.Setenv("LOG_TO_FILE", "true")
os.Setenv("LOG_CALLER_DEEP", "2")
os.Setenv("LOG_FILE_PATH", "/tmp/test.log")
err := ioc.ConfigIocObject(ioc.NewLoadConfigRequest())
if err != nil {
panic(err)
Expand Down
14 changes: 14 additions & 0 deletions ioc/config/logger/test/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[log]
caller_deep = 3
level = "debug"
trace_filed = "trace_id"
[log.console]
enable = true
no_color = false
[log.file]
enable = false
file_path = ""
max_size = 100
max_backups = 6
max_age = 0
compress = false
2 changes: 1 addition & 1 deletion ioc/config/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func init() {
ioc.Config().Registry(&mongoDB{
Database: "mcube",
Database: application.App().Name(),
AuthDB: "admin",
Endpoints: []string{"127.0.0.1:27017"},
EnableTrace: true,
Expand Down
2 changes: 0 additions & 2 deletions ioc/config/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

func init() {
ioc.Config().Registry(&Redist{
UserName: "root",
Password: "123456",
Database: 0,
Endpoints: []string{"127.0.0.1:6379"},
EnableTrace: true,
Expand Down

0 comments on commit 1576a17

Please sign in to comment.