yao/engine/load.go
2022-02-09 08:59:16 +08:00

111 lines
2.9 KiB
Go

package engine
import (
"fmt"
"os"
"path/filepath"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/xiang/api"
"github.com/yaoapp/xiang/app"
"github.com/yaoapp/xiang/chart"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/flow"
"github.com/yaoapp/xiang/importer"
"github.com/yaoapp/xiang/model"
"github.com/yaoapp/xiang/page"
"github.com/yaoapp/xiang/plugin"
"github.com/yaoapp/xiang/query"
"github.com/yaoapp/xiang/script"
"github.com/yaoapp/xiang/server"
"github.com/yaoapp/xiang/share"
"github.com/yaoapp/xiang/table"
"github.com/yaoapp/xiang/workflow"
)
// Load 根据配置加载 API, FLow, Model, Plugin
func Load(cfg config.Config) {
share.DBConnect(cfg.DB) // 创建数据库连接
share.SessionConnect(cfg.Session) // 创建会话服务器链接
app.Load(cfg) // 加载应用信息
if os.Getenv("YAO_DEV") != "" {
LoadEngine(filepath.Join(os.Getenv("YAO_DEV"), "/xiang"))
} else {
LoadEngine()
}
query.Load(cfg) // 加载数据分析引擎
share.Load(cfg) // 加载共享库 lib
script.Load(cfg) // 加载JS处理器 script
model.Load(cfg) // 加载数据模型 model
flow.Load(cfg) // 加载业务逻辑 Flow
plugin.Load(cfg) // 加载业务插件 plugin
table.Load(cfg) // 加载数据表格 table
chart.Load(cfg) // 加载分析图表 chart
page.Load(cfg) // 加载页面 page
importer.Load(cfg) // 加载数据导入 imports
workflow.Load(cfg) // 加载工作流 workflow
api.Load(cfg) // 加载业务接口 API
server.Load(cfg) // 加载服务
// 加密密钥函数
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.DB.AESKey), "AES")
gou.LoadCrypt(`{}`, "PASSWORD")
}
// Reload 根据配置重新加载 API, FLow, Model, Plugin
func Reload(cfg config.Config) {
gou.APIs = map[string]*gou.API{}
gou.Models = map[string]*gou.Model{}
gou.Flows = map[string]*gou.Flow{}
gou.Plugins = map[string]*gou.Plugin{}
Load(cfg)
}
// LoadEngine 加载引擎的 API, Flow, Model 配置
func LoadEngine(from ...string) {
var scripts []share.Script
if len(from) > 0 {
scripts = share.GetFilesFS(from[0], ".json")
} else {
scripts = share.GetFilesBin("/xiang", ".json")
}
if scripts == nil {
exception.New("读取文件失败", 500, from).Throw()
}
if len(scripts) == 0 {
exception.New("读取文件失败, 未找到任何可执行脚本", 500, from).Throw()
}
// 加载 API, Flow, Models, Table, Chart, Screens
for _, script := range scripts {
switch script.Type {
case "models":
gou.LoadModel(string(script.Content), "xiang."+script.Name)
break
case "flows":
gou.LoadFlow(string(script.Content), "xiang."+script.Name)
break
case "apis":
gou.LoadAPI(string(script.Content), "xiang."+script.Name)
break
}
}
// 加载数据应用
for _, script := range scripts {
switch script.Type {
case "tables":
table.LoadTable(string(script.Content), "xiang."+script.Name)
break
}
}
}