From 9cffe860d8e65e4f8e719caa40ffc52e98ad0298 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 18 Oct 2021 23:42:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BD=BD=E5=85=A5=E9=80=BB?= =?UTF-8?q?=E8=BE=91:=20Step=203/5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine/load.go | 56 +++++++++++++++++++---------------- flow/flow.go | 5 +--- flow/flow_test.go | 2 +- model/init_test.go | 22 ++++++++++++++ model/model.go | 24 ++++++++++++++- model/model_test.go | 27 +++++++++++++++++ share/db.go | 23 ++++++++++++++ tests/models/foo/bar.mod.json | 43 +++++++++++++++++++++++++++ 8 files changed, 170 insertions(+), 32 deletions(-) create mode 100644 model/init_test.go create mode 100644 model/model_test.go create mode 100644 share/db.go create mode 100644 tests/models/foo/bar.mod.json diff --git a/engine/load.go b/engine/load.go index 71dcd4ed..21a15959 100644 --- a/engine/load.go +++ b/engine/load.go @@ -13,18 +13,22 @@ import ( "github.com/yaoapp/xiang/app" "github.com/yaoapp/xiang/config" "github.com/yaoapp/xiang/flow" + "github.com/yaoapp/xiang/model" "github.com/yaoapp/xiang/share" "github.com/yaoapp/xiang/table" - "github.com/yaoapp/xun/capsule" ) // Load 根据配置加载 API, FLow, Model, Plugin func Load(cfg config.Config) { + + share.DBConnect(cfg.Database) // 创建数据库连接 + app.Load(cfg) // 加载应用信息 - DBConnect(cfg.Database) + LoadEngine(cfg.Path) - api.Load(cfg) // 加载API - flow.Load(cfg) // 加载Flow + model.Load(cfg) // 加载数据模型 + api.Load(cfg) // 加载API + flow.Load(cfg) // 加载Flow LoadApp(share.AppRoot{ APIs: cfg.RootAPI, Flows: cfg.RootFLow, @@ -50,22 +54,22 @@ func Reload(cfg config.Config) { Load(cfg) } -// DBConnect 建立数据库连接 -func DBConnect(dbconfig config.DatabaseConfig) { +// // DBConnect 建立数据库连接 +// func DBConnect(dbconfig config.DatabaseConfig) { - // 连接主库 - for i, dsn := range dbconfig.Primary { - db := capsule.AddConn("primary", dbconfig.Driver, dsn) - if i == 0 { - db.SetAsGlobal() - } - } +// // 连接主库 +// for i, dsn := range dbconfig.Primary { +// db := capsule.AddConn("primary", dbconfig.Driver, dsn) +// if i == 0 { +// db.SetAsGlobal() +// } +// } - // 连接从库 - for _, dsn := range dbconfig.Secondary { - capsule.AddReadConn("secondary", dbconfig.Driver, dsn) - } -} +// // 连接从库 +// for _, dsn := range dbconfig.Secondary { +// capsule.AddReadConn("secondary", dbconfig.Driver, dsn) +// } +// } // LoadEngine 加载引擎的 API, Flow, Model 配置 func LoadEngine(from string) { @@ -155,14 +159,14 @@ func LoadApp(app share.AppRoot) { // } // } - // 加载Model - if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") { - root := strings.TrimPrefix(app.Models, "fs://") - scripts := share.GetAppFilesFS(root, ".json") - for _, script := range scripts { - gou.LoadModel(string(script.Content), script.Name) - } - } + // // 加载Model + // if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") { + // root := strings.TrimPrefix(app.Models, "fs://") + // scripts := share.GetAppFilesFS(root, ".json") + // for _, script := range scripts { + // gou.LoadModel(string(script.Content), script.Name) + // } + // } // 加载Plugin if strings.HasPrefix(app.Plugins, "fs://") || !strings.Contains(app.Plugins, "://") { diff --git a/flow/flow.go b/flow/flow.go index 22c743b7..422cc4c3 100644 --- a/flow/flow.go +++ b/flow/flow.go @@ -1,16 +1,13 @@ package flow import ( - "fmt" - "github.com/yaoapp/gou" "github.com/yaoapp/xiang/config" "github.com/yaoapp/xiang/share" ) -// Load 加载API +// Load 加载业务逻辑编排 func Load(cfg config.Config) { - fmt.Println(cfg.RootFLow) LoadFrom(cfg.RootFLow, "") } diff --git a/flow/flow_test.go b/flow/flow_test.go index c819c0e8..5231567e 100644 --- a/flow/flow_test.go +++ b/flow/flow_test.go @@ -3,7 +3,7 @@ package flow import ( "testing" - "github.com/go-playground/assert/v2" + "github.com/stretchr/testify/assert" "github.com/yaoapp/gou" "github.com/yaoapp/xiang/config" ) diff --git a/model/init_test.go b/model/init_test.go new file mode 100644 index 00000000..9af906af --- /dev/null +++ b/model/init_test.go @@ -0,0 +1,22 @@ +package model + +import ( + "os" + "testing" + + "github.com/yaoapp/gou" + "github.com/yaoapp/xiang/config" +) + +var cfg config.Config + +func TestMain(m *testing.M) { + + // Run test suites + exitVal := m.Run() + + // we can do clean up code here + gou.KillPlugins() + + os.Exit(exitVal) +} diff --git a/model/model.go b/model/model.go index ddbba5f1..37a38145 100644 --- a/model/model.go +++ b/model/model.go @@ -1,4 +1,26 @@ package model +import ( + "github.com/yaoapp/gou" + "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xiang/share" +) + // Load 加载数据模型 -func Load(filepath string) {} +func Load(cfg config.Config) { + LoadFrom(cfg.RootModel, "") +} + +// LoadFrom 从特定目录加载 +func LoadFrom(dir string, prefix string) { + + if share.DirNotExists(dir) { + return + } + + share.Walk(dir, ".json", func(root, filename string) { + name := share.SpecName(root, filename) + content := share.ReadFile(filename) + gou.LoadModel(string(content), prefix+name) + }) +} diff --git a/model/model_test.go b/model/model_test.go new file mode 100644 index 00000000..d721d9a2 --- /dev/null +++ b/model/model_test.go @@ -0,0 +1,27 @@ +package model + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/yaoapp/gou" + "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xiang/share" +) + +func TestLoad(t *testing.T) { + share.DBConnect(config.Conf.Database) + gou.Models = make(map[string]*gou.Model) + Load(config.Conf) + LoadFrom("not a path", "404.") + check(t) +} + +func check(t *testing.T) { + keys := []string{} + for key := range gou.Models { + keys = append(keys, key) + } + assert.Equal(t, 9, len(keys)) +} diff --git a/share/db.go b/share/db.go new file mode 100644 index 00000000..2866c126 --- /dev/null +++ b/share/db.go @@ -0,0 +1,23 @@ +package share + +import ( + "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xun/capsule" +) + +// DBConnect 建立数据库连接 +func DBConnect(dbconfig config.DatabaseConfig) { + + // 连接主库 + for i, dsn := range dbconfig.Primary { + db := capsule.AddConn("primary", dbconfig.Driver, dsn) + if i == 0 { + db.SetAsGlobal() + } + } + + // 连接从库 + for _, dsn := range dbconfig.Secondary { + capsule.AddReadConn("secondary", dbconfig.Driver, dsn) + } +} diff --git a/tests/models/foo/bar.mod.json b/tests/models/foo/bar.mod.json new file mode 100644 index 00000000..9ee753fe --- /dev/null +++ b/tests/models/foo/bar.mod.json @@ -0,0 +1,43 @@ +{ + "name": "用户角色", + "table": { + "name": "foo_bar", + "comment": "用户角色关系表", + "engine": "InnoDB" + }, + "columns": [ + { "name": "id", "type": "ID" }, + { + "label": "用户ID", + "name": "user_id", + "type": "bigInteger", + "comment": "所属用户", + "index": true + }, + { + "label": "角色ID", + "name": "role_id", + "type": "bigInteger", + "comment": "角色ID", + "index": true + }, + { + "label": "状态", + "name": "status", + "type": "enum", + "default": "enabled", + "option": ["enabled", "disabled"], + "comment": "状态 enabled 开启, disabled 关闭", + "index": true + } + ], + "relations": {}, + "option": { "timestamps": true, "soft_deletes": true }, + "values": [ + { "user_id": 1, "role_id": 1 }, + { "user_id": 1, "role_id": 2 }, + { "user_id": 2, "role_id": 3 }, + { "user_id": 2, "role_id": 4 }, + { "user_id": 3, "role_id": 1 } + ] +}