forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
235 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/xiang/config" | ||
"github.com/yaoapp/xiang/share" | ||
) | ||
|
||
// Load 加载API | ||
func Load(cfg config.Config) { | ||
LoadFrom(cfg.RootAPI, "") | ||
} | ||
|
||
// 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.LoadAPI(string(content), prefix+name) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/xiang/config" | ||
) | ||
|
||
func TestLoad(t *testing.T) { | ||
gou.APIs = make(map[string]*gou.API) | ||
Load(config.Conf) | ||
LoadFrom("not a path", "404.") | ||
check(t) | ||
} | ||
|
||
func check(t *testing.T) { | ||
keys := []string{} | ||
for key := range gou.APIs { | ||
keys = append(keys, key) | ||
} | ||
assert.Equal(t, 3, len(keys)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package app | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/yaoapp/kun/exception" | ||
"github.com/yaoapp/kun/maps" | ||
"github.com/yaoapp/xiang/config" | ||
"github.com/yaoapp/xiang/data" | ||
"github.com/yaoapp/xiang/share" | ||
"github.com/yaoapp/xiang/xfs" | ||
) | ||
|
||
// Load 加载应用信息 | ||
func Load(cfg config.Config) { | ||
Init(cfg) | ||
LoadInfo(cfg.Root) | ||
} | ||
|
||
// Init 应用初始化 | ||
func Init(cfg config.Config) { | ||
if _, err := os.Stat(cfg.RootUI); os.IsNotExist(err) { | ||
err := os.MkdirAll(cfg.RootUI, os.ModePerm) | ||
if err != nil { | ||
log.Panicf("创建目录失败(%s) %s", cfg.RootUI, err) | ||
} | ||
} | ||
|
||
if _, err := os.Stat(cfg.RootDB); os.IsNotExist(err) { | ||
err := os.MkdirAll(cfg.RootDB, os.ModePerm) | ||
if err != nil { | ||
log.Panicf("创建目录失败(%s) %s", cfg.RootDB, err) | ||
} | ||
} | ||
|
||
if _, err := os.Stat(cfg.RootData); os.IsNotExist(err) { | ||
err := os.MkdirAll(cfg.RootData, os.ModePerm) | ||
if err != nil { | ||
log.Panicf("创建目录失败(%s) %s", cfg.RootData, err) | ||
} | ||
} | ||
} | ||
|
||
// LoadInfo 应用信息 | ||
func LoadInfo(root string) { | ||
info := defaultInfo() | ||
fs := xfs.New(root) | ||
if fs.MustExists("/app.json") { | ||
err := jsoniter.Unmarshal(fs.MustReadFile("/app.json"), &info) | ||
if err != nil { | ||
exception.New("解析应用失败 %s", 500, err).Throw() | ||
} | ||
} | ||
|
||
if fs.MustExists("/xiang/icons/icon.icns") { | ||
info.Icons.Set("icns", xfs.Encode(fs.MustReadFile("/xiang/icons/icon.icns"))) | ||
} | ||
|
||
if fs.MustExists("/xiang/icons/icon.ico") { | ||
info.Icons.Set("ico", xfs.Encode(fs.MustReadFile("/xiang/icons/icon.ico"))) | ||
} | ||
|
||
if fs.MustExists("/xiang/icons/icon.png") { | ||
info.Icons.Set("png", xfs.Encode(fs.MustReadFile("/xiang/icons/icon.png"))) | ||
} | ||
|
||
share.App = info | ||
} | ||
|
||
// defaultInfo 读取默认应用信息 | ||
func defaultInfo() share.AppInfo { | ||
info := share.AppInfo{ | ||
Icons: maps.MakeSync(), | ||
} | ||
err := jsoniter.Unmarshal(data.MustAsset("xiang/data/app.json"), &info) | ||
if err != nil { | ||
exception.New("解析默认应用失败 %s", 500, err).Throw() | ||
} | ||
|
||
info.Icons.Set("icns", xfs.Encode(data.MustAsset("xiang/data/icons/icon.icns"))) | ||
info.Icons.Set("ico", xfs.Encode(data.MustAsset("xiang/data/icons/icon.ico"))) | ||
info.Icons.Set("png", xfs.Encode(data.MustAsset("xiang/data/icons/icon.png"))) | ||
|
||
return info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package flow | ||
|
||
// Load 加载业务逻辑编排 | ||
func Load(filepath string) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package model | ||
|
||
// Load 加载数据模型 | ||
func Load(filepath string) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package plugin | ||
|
||
// Load 加载应用插件 | ||
func Load(filepath string) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package share | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/yaoapp/kun/exception" | ||
) | ||
|
||
// Walk 遍历应用目录,读取文件列表 | ||
func Walk(root string, typeName string, cb func(root, filename string)) { | ||
root = strings.TrimPrefix(root, "fs://") | ||
root = strings.TrimPrefix(root, "file://") | ||
root = path.Join(root, "/") | ||
filepath.Walk(root, func(filename string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
exception.Err(err, 500).Throw() | ||
return err | ||
} | ||
if strings.HasSuffix(filename, typeName) { | ||
cb(root, filename) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
// SpecName 解析名称 root: "/tests/apis" file: "/tests/apis/foo/bar.http.json" | ||
func SpecName(root string, file string) string { | ||
filename := strings.TrimPrefix(file, root+"/") // "foo/bar.http.json" | ||
namer := strings.Split(filename, ".") // ["foo/bar", "http", "json"] | ||
nametypes := strings.Split(namer[0], "/") // ["foo", "bar"] | ||
name := strings.Join(nametypes, ".") // "foo.bar" | ||
return name | ||
} | ||
|
||
// ReadFile 读取文件 | ||
func ReadFile(filename string) []byte { | ||
file, err := os.Open(filename) | ||
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 content | ||
} | ||
|
||
// DirNotExists 校验目录是否存在 | ||
func DirNotExists(dir string) bool { | ||
dir = strings.TrimPrefix(dir, "fs://") | ||
dir = strings.TrimPrefix(dir, "file://") | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "厂商接口", | ||
"version": "1.0.0", | ||
"description": "厂商API", | ||
"group": "manu", | ||
"guard": "bearer-jwt", | ||
"paths": [] | ||
} |