Skip to content

Commit

Permalink
优化代码结构 v0.8.8
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 18, 2021
1 parent 7b87f3f commit d03e1fc
Show file tree
Hide file tree
Showing 28 changed files with 653 additions and 617 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GOFILES := $(shell find . -name "*.go")
VERSION := $(shell grep 'const VERSION =' share/vars.go |awk '{print $$4}' |sed 's/\"//g')

# ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'xiang$$|global$$|table$$|user$$|xfs$$' | grep -v examples)
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'xiang$$|entry$$|table$$|user$$|xfs$$' | grep -v examples)
TESTTAGS ?= ""

# 运行单元测试
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/global"
"github.com/yaoapp/xiang/entry"
)

var name string
Expand All @@ -18,7 +18,7 @@ var migrateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
Boot()
// 加载数据模型
global.Load(config.Conf)
entry.Load(config.Conf)

if name != "" {
mod, has := gou.Models[name]
Expand Down
11 changes: 6 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/global"
"github.com/yaoapp/xiang/entry"
"github.com/yaoapp/xiang/service"
"github.com/yaoapp/xiang/share"
)

Expand All @@ -18,7 +19,7 @@ var startCmd = &cobra.Command{
Short: "启动象传应用引擎",
Long: `启动象传应用引擎`,
Run: func(cmd *cobra.Command, args []string) {
defer global.ServiceStop(func() { fmt.Println("服务已关闭") })
defer service.Stop(func() { fmt.Println("服务已关闭") })

Boot()
mode := config.Conf.Mode
Expand All @@ -31,7 +32,7 @@ var startCmd = &cobra.Command{
fmt.Printf(color.GreenString("\n象传应用引擎 v%s %s", share.VERSION, mode))

// 加载数据模型 API 等
global.Load(config.Conf)
entry.Load(config.Conf)

// 打印应用目录信息
fmt.Printf(color.WhiteString("\n---------------------------------"))
Expand Down Expand Up @@ -78,10 +79,10 @@ var startCmd = &cobra.Command{

// 调试模式
if config.Conf.Mode == "debug" {
global.WatchChanges()
service.WatchChanges()
}

global.ServiceStart()
service.Start()
},
}

Expand Down
3 changes: 3 additions & 0 deletions entry/READE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 程序入口

加载各种文件
2 changes: 1 addition & 1 deletion global/init_test.go → entry/init_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global
package entry

import (
"os"
Expand Down
222 changes: 11 additions & 211 deletions global/load.go → entry/load.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package global
package entry

import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -21,37 +19,14 @@ import (
"github.com/yaoapp/xun/capsule"
)

// Conf 配置文件
var Conf config.Config

// Script 脚本文件类型
type Script struct {
Name string
Type string
Content []byte
File string
}

// AppRoot 应用目录
type AppRoot struct {
APIs string
Flows string
Models string
Plugins string
Tables string
Charts string
Screens string
Data string
}

// Load 根据配置加载 API, FLow, Model, Plugin
func Load(cfg config.Config) {

AppInit(cfg)
DBConnect(cfg.Database)
LoadAppInfo(cfg.Root)
LoadEngine(cfg.Path)
LoadApp(AppRoot{
LoadApp(share.AppRoot{
APIs: cfg.RootAPI,
Flows: cfg.RootFLow,
Models: cfg.RootModel,
Expand All @@ -65,9 +40,6 @@ func Load(cfg config.Config) {
// 加密密钥函数
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES")
gou.LoadCrypt(`{}`, "PASSWORD")

// 设定已加载配置
Conf = cfg
}

// LoadAppInfo 读取应用信息
Expand Down Expand Up @@ -167,13 +139,13 @@ func AppInit(cfg config.Config) {
// LoadEngine 加载引擎的 API, Flow, Model 配置
func LoadEngine(from string) {

var scripts []Script
var scripts []share.Script
if strings.HasPrefix(from, "fs://") || !strings.Contains(from, "://") {
root := strings.TrimPrefix(from, "fs://")
scripts = getFilesFS(root, ".json")
scripts = share.GetFilesFS(root, ".json")
} else if strings.HasPrefix(from, "bin://") {
root := strings.TrimPrefix(from, "bin://")
scripts = getFilesBin(root, ".json")
scripts = share.GetFilesBin(root, ".json")
}

if scripts == nil {
Expand Down Expand Up @@ -210,7 +182,7 @@ func LoadEngine(from string) {
}

// LoadApp 加载应用的 API, Flow, Model 和 Plugin
func LoadApp(app AppRoot) {
func LoadApp(app share.AppRoot) {

// api string, flow string, model string, plugin string
// 创建应用目录
Expand All @@ -235,7 +207,7 @@ func LoadApp(app AppRoot) {
// 加载API
if strings.HasPrefix(app.APIs, "fs://") || !strings.Contains(app.APIs, "://") {
root := strings.TrimPrefix(app.APIs, "fs://")
scripts := getAppFilesFS(root, ".json")
scripts := share.GetAppFilesFS(root, ".json")
for _, script := range scripts {
// 验证API 加载逻辑
gou.LoadAPI(string(script.Content), script.Name)
Expand All @@ -245,7 +217,7 @@ func LoadApp(app AppRoot) {
// 加载Flow
if strings.HasPrefix(app.Flows, "fs://") || !strings.Contains(app.Flows, "://") {
root := strings.TrimPrefix(app.Flows, "fs://")
scripts := getAppFilesFS(root, ".json")
scripts := share.GetAppFilesFS(root, ".json")
for _, script := range scripts {
gou.LoadFlow(string(script.Content), script.Name)
}
Expand All @@ -254,7 +226,7 @@ func LoadApp(app AppRoot) {
// 加载Model
if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") {
root := strings.TrimPrefix(app.Models, "fs://")
scripts := getAppFilesFS(root, ".json")
scripts := share.GetAppFilesFS(root, ".json")
for _, script := range scripts {
gou.LoadModel(string(script.Content), script.Name)
}
Expand All @@ -263,7 +235,7 @@ func LoadApp(app AppRoot) {
// 加载Plugin
if strings.HasPrefix(app.Plugins, "fs://") || !strings.Contains(app.Plugins, "://") {
root := strings.TrimPrefix(app.Plugins, "fs://")
scripts := getAppPlugins(root, ".so")
scripts := share.GetAppPlugins(root, ".so")
for _, script := range scripts {
gou.LoadPlugin(script.File, script.Name)
}
Expand All @@ -272,183 +244,11 @@ func LoadApp(app AppRoot) {
// 加载Table
if strings.HasPrefix(app.Tables, "fs://") || !strings.Contains(app.Tables, "://") {
root := strings.TrimPrefix(app.Tables, "fs://")
scripts := getAppFilesFS(root, ".json")
scripts := share.GetAppFilesFS(root, ".json")
for _, script := range scripts {
// 验证API 加载逻辑
table.Load(string(script.Content), script.Name)
}
}

}

// / getAppPluins 遍历应用目录,读取文件列表
func getAppPlugins(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(file string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(file, typ) {
files = append(files, getAppPluginFile(root, file))
}
return nil
})
return files
}

// getAppPluginFile 读取文件
func getAppPluginFile(root string, file string) Script {
name := getAppPluginFileName(root, file)
return Script{
Name: name,
Type: "plugin",
File: file,
}
}

// getAppFile 读取文件
func getAppPluginFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
nametypes := strings.Split(namer[0], "/")
name := strings.Join(nametypes, ".")
return name
}

// 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) {
files = append(files, getAppFile(root, filepath))
}

return nil
})
return files
}

// getAppFile 读取文件
func getAppFile(root string, filepath string) Script {
name := getAppFileName(root, filepath)
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()
}
return Script{
Name: name,
Type: "app",
Content: content,
}
}

// getAppFile 读取文件
func getAppFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
nametypes := strings.Split(namer[0], "/")
name := strings.Join(nametypes, ".")
return name
}

// getAppFileBaseName 读取文件base
func getAppFileBaseName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
return filepath.Join(root, namer[0])
}

// getFilesFS 遍历目录,读取文件列表
func getFilesFS(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(path, typ) {
files = append(files, getFile(root, path))
}
return nil
})
return files
}

// getFile 读取文件
func getFile(root string, path string) Script {
filename := strings.TrimPrefix(path, root+"/")
name, typ := getTypeName(filename)
file, err := os.Open(path)
if err != nil {
exception.Err(err, 500).Throw()
}

defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
exception.Err(err, 500).Throw()
}
return Script{
Name: name,
Type: typ,
Content: content,
}
}

// getFileName 读取文件
func getFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
name, _ := getTypeName(filename)
return name
}

// getFileBaseName 读取文件base
func getFileBaseName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
return filepath.Join(root, namer[0])
}

// getFilesBin 从 bindata 中读取文件列表
func getFilesBin(root string, typ string) []Script {
files := []Script{}
binfiles := data.AssetNames()
for _, path := range binfiles {
if strings.HasSuffix(path, typ) {
file := strings.TrimPrefix(path, root+"/")
name, typ := getTypeName(file)
content, err := data.Asset(path)
if err != nil {
exception.Err(err, 500).Throw()
}
files = append(files, Script{
Name: name,
Type: typ,
Content: content,
})
}
}
return files
}

func getTypeName(path string) (name string, typ string) {
namer := strings.Split(path, ".")
nametypes := strings.Split(namer[0], "/")
name = strings.Join(nametypes[1:], ".")
typ = nametypes[0]
return name, typ
}
Loading

0 comments on commit d03e1fc

Please sign in to comment.