+ Load()
This commit is contained in:
parent
ee1872bdea
commit
6a3034e6d4
5 changed files with 141 additions and 10 deletions
11
Makefile
11
Makefile
|
|
@ -85,15 +85,16 @@ plugin:
|
||||||
rm -rf $(HOME)/data/gou-unit/logs
|
rm -rf $(HOME)/data/gou-unit/logs
|
||||||
mkdir -p $(HOME)/data/gou-unit/plugins
|
mkdir -p $(HOME)/data/gou-unit/plugins
|
||||||
mkdir -p $(HOME)/data/gou-unit/logs
|
mkdir -p $(HOME)/data/gou-unit/logs
|
||||||
GOOS=linux GOARCH=amd64 go build -o $(HOME)/data/gou-unit/plugins/user ./app/plugins/user
|
GOOS=linux GOARCH=amd64 go build -o $(HOME)/data/gou-unit/plugins/user.so ./app/plugins/user
|
||||||
chmod +x $(HOME)/data/gou-unit/plugins/user
|
chmod +x $(HOME)/data/gou-unit/plugins/user.so
|
||||||
ls -l $(HOME)/data/gou-unit/plugins
|
ls -l $(HOME)/data/gou-unit/plugins
|
||||||
ls -l $(HOME)/data/gou-unit/logs
|
ls -l $(HOME)/data/gou-unit/logs
|
||||||
$(HOME)/data/gou-unit/plugins/user 2>&1 || true
|
$(HOME)/data/gou-unit/plugins/user.so 2>&1 || true
|
||||||
plugin-mac:
|
plugin-mac:
|
||||||
rm -rf ./app/plugins/user/dist
|
rm -rf ./app/plugins/user/dist
|
||||||
go build -o ./app/plugins/dist/user ./app/plugins/user
|
rm -rf ./app/plugins/user.so
|
||||||
chmod +x ./app/plugins/dist/user
|
go build -o ./app/plugins/user.so ./app/plugins/user
|
||||||
|
chmod +x ./app/plugins/user.so
|
||||||
|
|
||||||
# 编译静态文件
|
# 编译静态文件
|
||||||
.PHONY: static
|
.PHONY: static
|
||||||
|
|
|
||||||
18
config.go
18
config.go
|
|
@ -89,6 +89,7 @@ func NewConfig() Config {
|
||||||
exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
|
exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cfg.SetDefaults()
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,5 +100,22 @@ func NewConfigFrom(input io.Reader) Config {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
|
exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
|
||||||
}
|
}
|
||||||
|
cfg.SetDefaults()
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDefaults 设定默认值
|
||||||
|
func (cfg *Config) SetDefaults() {
|
||||||
|
if cfg.RootAPI == "" {
|
||||||
|
cfg.RootAPI = cfg.Root + "/apis"
|
||||||
|
}
|
||||||
|
if cfg.RootFLow == "" {
|
||||||
|
cfg.RootFLow = cfg.Root + "/flows"
|
||||||
|
}
|
||||||
|
if cfg.RootModel == "" {
|
||||||
|
cfg.RootModel = cfg.Root + "/models"
|
||||||
|
}
|
||||||
|
if cfg.RootPlugin == "" {
|
||||||
|
cfg.RootPlugin = cfg.Root + "/plugins"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
108
load.go
108
load.go
|
|
@ -16,14 +16,17 @@ type Script struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
Content []byte
|
Content []byte
|
||||||
|
File string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load 根据配置加载 API, FLow, Model, Plugin
|
// Load 根据配置加载 API, FLow, Model, Plugin
|
||||||
func Load() error {
|
func Load(cfg Config) error {
|
||||||
|
LoadEngine(cfg.Path)
|
||||||
|
LoadApp(cfg.RootAPI, cfg.RootFLow, cfg.RootModel, cfg.RootPlugin)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadEngine 载入引擎 API, Flow, Model 配置
|
// LoadEngine 加载引擎的 API, Flow, Model 配置
|
||||||
func LoadEngine(from string) {
|
func LoadEngine(from string) {
|
||||||
|
|
||||||
var scripts []Script
|
var scripts []Script
|
||||||
|
|
@ -57,7 +60,108 @@ func LoadEngine(from string) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadApp 加载应用的 API, Flow, Model 和 Plugin
|
||||||
|
func LoadApp(api string, flow string, model string, plugin string) {
|
||||||
|
|
||||||
|
// 加载API
|
||||||
|
if strings.HasPrefix(api, "fs://") || !strings.Contains(api, "://") {
|
||||||
|
root := strings.TrimPrefix(api, "fs://")
|
||||||
|
scripts := getAppFilesFS(root, ".json")
|
||||||
|
for _, script := range scripts {
|
||||||
|
gou.LoadAPI(string(script.Content), script.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载Flow
|
||||||
|
if strings.HasPrefix(flow, "fs://") || !strings.Contains(flow, "://") {
|
||||||
|
root := strings.TrimPrefix(flow, "fs://")
|
||||||
|
scripts := getAppFilesFS(root, ".json")
|
||||||
|
for _, script := range scripts {
|
||||||
|
gou.LoadFlow(string(script.Content), script.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载Model
|
||||||
|
if strings.HasPrefix(model, "fs://") || !strings.Contains(model, "://") {
|
||||||
|
root := strings.TrimPrefix(model, "fs://")
|
||||||
|
scripts := getAppFilesFS(root, ".json")
|
||||||
|
for _, script := range scripts {
|
||||||
|
gou.LoadModel(string(script.Content), script.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载Plugin
|
||||||
|
if strings.HasPrefix(plugin, "fs://") || !strings.Contains(plugin, "://") {
|
||||||
|
root := strings.TrimPrefix(plugin, "fs://")
|
||||||
|
scripts := getAppPlugins(root, ".so")
|
||||||
|
for _, script := range scripts {
|
||||||
|
gou.LoadPlugin(script.File, script.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// / getAppPluins 遍历应用目录,读取文件列表
|
||||||
|
func getAppPlugins(root string, typ string) []Script {
|
||||||
|
files := []Script{}
|
||||||
|
root = path.Join(root, "/")
|
||||||
|
filepath.Walk(root, func(filepath string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
exception.Err(err, 500).Throw()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(filepath, typ) {
|
||||||
|
filename := strings.TrimPrefix(filepath, root+"/")
|
||||||
|
namer := strings.Split(filename, ".")
|
||||||
|
nametypes := strings.Split(namer[0], "/")
|
||||||
|
name := strings.Join(nametypes, ".")
|
||||||
|
files = append(files, Script{
|
||||||
|
Name: name,
|
||||||
|
Type: "plugin",
|
||||||
|
File: filepath,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
// getAppFilesFS 遍历应用目录,读取文件列表
|
||||||
|
func getAppFilesFS(root string, typ string) []Script {
|
||||||
|
files := []Script{}
|
||||||
|
root = path.Join(root, "/")
|
||||||
|
filepath.Walk(root, func(filepath string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
exception.Err(err, 500).Throw()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(filepath, typ) {
|
||||||
|
filename := strings.TrimPrefix(filepath, root+"/")
|
||||||
|
|
||||||
|
namer := strings.Split(filename, ".")
|
||||||
|
nametypes := strings.Split(namer[0], "/")
|
||||||
|
name := strings.Join(nametypes, ".")
|
||||||
|
|
||||||
|
file, err := os.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
exception.Err(err, 500).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
content, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
exception.Err(err, 500).Throw()
|
||||||
|
}
|
||||||
|
files = append(files, Script{
|
||||||
|
Name: name,
|
||||||
|
Type: "app",
|
||||||
|
Content: content,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
// getFilesFS 遍历目录,读取文件列表
|
// getFilesFS 遍历目录,读取文件列表
|
||||||
|
|
|
||||||
12
load_test.go
12
load_test.go
|
|
@ -8,8 +8,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
func TestLoad(t *testing.T) {
|
||||||
err := Load()
|
assert.NotPanics(t, func() {
|
||||||
assert.Nil(t, err)
|
Load(cfg)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从文件系统载入引擎文件
|
// 从文件系统载入引擎文件
|
||||||
|
|
@ -28,3 +29,10 @@ func TestLoadEngineBin(t *testing.T) {
|
||||||
LoadEngine(root)
|
LoadEngine(root)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从文件系统载入应用脚本
|
||||||
|
func TestLoadAppFS(t *testing.T) {
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
LoadApp(cfg.RootAPI, cfg.RootFLow, cfg.RootModel, cfg.RootPlugin)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMainCMD(t *testing.T) {
|
func TestCommand(t *testing.T) {
|
||||||
assert.NotPanics(t, func() {
|
assert.NotPanics(t, func() {
|
||||||
main()
|
main()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue