优化载入逻辑: Step 3/5
This commit is contained in:
parent
1d5f1d110f
commit
9cffe860d8
8 changed files with 170 additions and 32 deletions
|
|
@ -13,16 +13,20 @@ import (
|
||||||
"github.com/yaoapp/xiang/app"
|
"github.com/yaoapp/xiang/app"
|
||||||
"github.com/yaoapp/xiang/config"
|
"github.com/yaoapp/xiang/config"
|
||||||
"github.com/yaoapp/xiang/flow"
|
"github.com/yaoapp/xiang/flow"
|
||||||
|
"github.com/yaoapp/xiang/model"
|
||||||
"github.com/yaoapp/xiang/share"
|
"github.com/yaoapp/xiang/share"
|
||||||
"github.com/yaoapp/xiang/table"
|
"github.com/yaoapp/xiang/table"
|
||||||
"github.com/yaoapp/xun/capsule"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load 根据配置加载 API, FLow, Model, Plugin
|
// Load 根据配置加载 API, FLow, Model, Plugin
|
||||||
func Load(cfg config.Config) {
|
func Load(cfg config.Config) {
|
||||||
|
|
||||||
|
share.DBConnect(cfg.Database) // 创建数据库连接
|
||||||
|
|
||||||
app.Load(cfg) // 加载应用信息
|
app.Load(cfg) // 加载应用信息
|
||||||
DBConnect(cfg.Database)
|
|
||||||
LoadEngine(cfg.Path)
|
LoadEngine(cfg.Path)
|
||||||
|
model.Load(cfg) // 加载数据模型
|
||||||
api.Load(cfg) // 加载API
|
api.Load(cfg) // 加载API
|
||||||
flow.Load(cfg) // 加载Flow
|
flow.Load(cfg) // 加载Flow
|
||||||
LoadApp(share.AppRoot{
|
LoadApp(share.AppRoot{
|
||||||
|
|
@ -50,22 +54,22 @@ func Reload(cfg config.Config) {
|
||||||
Load(cfg)
|
Load(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBConnect 建立数据库连接
|
// // DBConnect 建立数据库连接
|
||||||
func DBConnect(dbconfig config.DatabaseConfig) {
|
// func DBConnect(dbconfig config.DatabaseConfig) {
|
||||||
|
|
||||||
// 连接主库
|
// // 连接主库
|
||||||
for i, dsn := range dbconfig.Primary {
|
// for i, dsn := range dbconfig.Primary {
|
||||||
db := capsule.AddConn("primary", dbconfig.Driver, dsn)
|
// db := capsule.AddConn("primary", dbconfig.Driver, dsn)
|
||||||
if i == 0 {
|
// if i == 0 {
|
||||||
db.SetAsGlobal()
|
// db.SetAsGlobal()
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 连接从库
|
// // 连接从库
|
||||||
for _, dsn := range dbconfig.Secondary {
|
// for _, dsn := range dbconfig.Secondary {
|
||||||
capsule.AddReadConn("secondary", dbconfig.Driver, dsn)
|
// capsule.AddReadConn("secondary", dbconfig.Driver, dsn)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// LoadEngine 加载引擎的 API, Flow, Model 配置
|
// LoadEngine 加载引擎的 API, Flow, Model 配置
|
||||||
func LoadEngine(from string) {
|
func LoadEngine(from string) {
|
||||||
|
|
@ -155,14 +159,14 @@ func LoadApp(app share.AppRoot) {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// 加载Model
|
// // 加载Model
|
||||||
if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") {
|
// if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") {
|
||||||
root := strings.TrimPrefix(app.Models, "fs://")
|
// root := strings.TrimPrefix(app.Models, "fs://")
|
||||||
scripts := share.GetAppFilesFS(root, ".json")
|
// scripts := share.GetAppFilesFS(root, ".json")
|
||||||
for _, script := range scripts {
|
// for _, script := range scripts {
|
||||||
gou.LoadModel(string(script.Content), script.Name)
|
// gou.LoadModel(string(script.Content), script.Name)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 加载Plugin
|
// 加载Plugin
|
||||||
if strings.HasPrefix(app.Plugins, "fs://") || !strings.Contains(app.Plugins, "://") {
|
if strings.HasPrefix(app.Plugins, "fs://") || !strings.Contains(app.Plugins, "://") {
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,13 @@
|
||||||
package flow
|
package flow
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/yaoapp/gou"
|
"github.com/yaoapp/gou"
|
||||||
"github.com/yaoapp/xiang/config"
|
"github.com/yaoapp/xiang/config"
|
||||||
"github.com/yaoapp/xiang/share"
|
"github.com/yaoapp/xiang/share"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load 加载API
|
// Load 加载业务逻辑编排
|
||||||
func Load(cfg config.Config) {
|
func Load(cfg config.Config) {
|
||||||
fmt.Println(cfg.RootFLow)
|
|
||||||
LoadFrom(cfg.RootFLow, "")
|
LoadFrom(cfg.RootFLow, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package flow
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/go-playground/assert/v2"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/gou"
|
"github.com/yaoapp/gou"
|
||||||
"github.com/yaoapp/xiang/config"
|
"github.com/yaoapp/xiang/config"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
22
model/init_test.go
Normal file
22
model/init_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,26 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/yaoapp/gou"
|
||||||
|
"github.com/yaoapp/xiang/config"
|
||||||
|
"github.com/yaoapp/xiang/share"
|
||||||
|
)
|
||||||
|
|
||||||
// Load 加载数据模型
|
// 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
27
model/model_test.go
Normal file
27
model/model_test.go
Normal file
|
|
@ -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))
|
||||||
|
}
|
||||||
23
share/db.go
Normal file
23
share/db.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
43
tests/models/foo/bar.mod.json
Normal file
43
tests/models/foo/bar.mod.json
Normal file
|
|
@ -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 }
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue