This commit is contained in:
Max 2021-09-08 19:38:12 +08:00
parent ee1872bdea
commit 6a3034e6d4
5 changed files with 141 additions and 10 deletions

View file

@ -85,15 +85,16 @@ plugin:
rm -rf $(HOME)/data/gou-unit/logs
mkdir -p $(HOME)/data/gou-unit/plugins
mkdir -p $(HOME)/data/gou-unit/logs
GOOS=linux GOARCH=amd64 go build -o $(HOME)/data/gou-unit/plugins/user ./app/plugins/user
chmod +x $(HOME)/data/gou-unit/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.so
ls -l $(HOME)/data/gou-unit/plugins
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:
rm -rf ./app/plugins/user/dist
go build -o ./app/plugins/dist/user ./app/plugins/user
chmod +x ./app/plugins/dist/user
rm -rf ./app/plugins/user.so
go build -o ./app/plugins/user.so ./app/plugins/user
chmod +x ./app/plugins/user.so
# 编译静态文件
.PHONY: static

View file

@ -89,6 +89,7 @@ func NewConfig() Config {
exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
}
cfg.SetDefaults()
return cfg
}
@ -99,5 +100,22 @@ func NewConfigFrom(input io.Reader) Config {
if err != nil {
exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
}
cfg.SetDefaults()
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
View file

@ -16,14 +16,17 @@ type Script struct {
Name string
Type string
Content []byte
File string
}
// 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
}
// LoadEngine 引擎 API, Flow, Model 配置
// LoadEngine 载引擎 API, Flow, Model 配置
func LoadEngine(from string) {
var scripts []Script
@ -57,7 +60,108 @@ func LoadEngine(from string) {
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 遍历目录,读取文件列表

View file

@ -8,8 +8,9 @@ import (
)
func TestLoad(t *testing.T) {
err := Load()
assert.Nil(t, err)
assert.NotPanics(t, func() {
Load(cfg)
})
}
// 从文件系统载入引擎文件
@ -28,3 +29,10 @@ func TestLoadEngineBin(t *testing.T) {
LoadEngine(root)
})
}
// 从文件系统载入应用脚本
func TestLoadAppFS(t *testing.T) {
assert.NotPanics(t, func() {
LoadApp(cfg.RootAPI, cfg.RootFLow, cfg.RootModel, cfg.RootPlugin)
})
}

View file

@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestMainCMD(t *testing.T) {
func TestCommand(t *testing.T) {
assert.NotPanics(t, func() {
main()
})