Skip to content

Commit

Permalink
v0.9.3 + socket server support ( yao start alpha )
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Jan 26, 2022
1 parent b9e8f97 commit 6372faf
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func LoadFrom(dir string, prefix string) {
return
}

share.Walk(dir, ".json", func(root, filename string) {
share.Walk(dir, ".http.json", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
gou.LoadAPI(string(content), name)
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(
versionCmd,
domainCmd,
// domainCmd,
migrateCmd,
inspectCmd,
startCmd,
importCmd,
// importCmd,
runCmd,
)
rootCmd.SetHelpCommand(helpCmd)
Expand Down
18 changes: 14 additions & 4 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,28 @@ var startCmd = &cobra.Command{
port = ""
}

fmt.Printf(color.GreenString("\n\n访问入口"))
fmt.Printf(color.GreenString("\n\n象传应用引擎"))
fmt.Printf(color.WhiteString("\n---------------------------------"))
fmt.Printf(color.GreenString("\n前台界面: http://%s%s/\n", domain, port))
fmt.Printf(color.GreenString("管理后台: http://%s%s/xiang/login\n", domain, port))
fmt.Printf(color.GreenString("API 接口: http://%s%s/api\n", domain, port))
fmt.Printf(color.GreenString("\n前台界面: http://%s%s/\n", "127.0.0.1", port))
fmt.Printf(color.GreenString("管理后台: http://%s%s/xiang/login/admin\n", "127.0.0.1", port))
fmt.Printf(color.GreenString("API 接口: http://%s%s/api\n", "127.0.0.1", port))
fmt.Printf(color.GreenString("跨域访问: %s\n\n", strings.Join(config.Conf.Service.Allow, ",")))

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

if len(args) > 0 && args[0] == "alpha" {
// 启动 Socket 服务 (alpha)
for _, srv := range gou.Servers {
fmt.Printf(color.GreenString("%s服务", srv.Name))
fmt.Printf(color.WhiteString("\n---------------------------------"))
fmt.Printf(color.GreenString("\nHost: %s://%s", srv.Protocol, srv.Host))
fmt.Printf(color.GreenString("\nPort: %s\n\n", srv.Port))
go srv.Start()
}
}
service.Start()
},
}
Expand Down
2 changes: 2 additions & 0 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/yaoapp/xiang/page"
"github.com/yaoapp/xiang/plugin"
"github.com/yaoapp/xiang/query"
"github.com/yaoapp/xiang/server"
"github.com/yaoapp/xiang/share"
"github.com/yaoapp/xiang/table"
"github.com/yaoapp/xiang/workflow"
Expand All @@ -42,6 +43,7 @@ func Load(cfg config.Config) {
importer.Load(cfg) // 加载数据导入 imports
workflow.Load(cfg) // 加载工作流 workflow
api.Load(cfg) // 加载业务接口 API
server.Load(cfg) // 加载服务

// 加密密钥函数
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES")
Expand Down
2 changes: 1 addition & 1 deletion flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func check(t *testing.T) {
for key := range gou.Flows {
keys = append(keys, key)
}
assert.Equal(t, 21, len(keys))
assert.Equal(t, 22, len(keys))
}
29 changes: 29 additions & 0 deletions server/socket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package server

import (
"path/filepath"

"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/share"
)

// Load 加载API
func Load(cfg config.Config) {
var root = filepath.Join(cfg.RootAPI, "..", "servers")
LoadFrom(root, "")
}

// LoadFrom 从特定目录加载
func LoadFrom(dir string, prefix string) {

if share.DirNotExists(dir) {
return
}

share.Walk(dir, ".sock.json", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
gou.LoadServer(string(content), name)
})
}
23 changes: 23 additions & 0 deletions server/socket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package server

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
)

func TestLoad(t *testing.T) {
Load(config.Conf)
LoadFrom("not a path", "404.")
check(t)
}

func check(t *testing.T) {
keys := []string{}
for key := range gou.Servers {
keys = append(keys, key)
}
assert.Equal(t, 1, len(keys))
}
2 changes: 1 addition & 1 deletion share/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package share

// VERSION 版本号
const VERSION = "0.9.2"
const VERSION = "0.9.3"

// DOMAIN 许可域(废弃)
const DOMAIN = "*.iqka.com"
Expand Down
7 changes: 7 additions & 0 deletions tests/flows/rfid/read.flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"label": "读取RFID标签",
"version": "1.0.0",
"description": "读取RFID标签",
"nodes": [{ "script": "print" }],
"output": null
}
11 changes: 11 additions & 0 deletions tests/flows/rfid/read.print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function main(args, out, res) {
// 100000012021128798321101 15 2d 02 f2 96 72 1e 52 b9 cd
var idstr = args[0] || "";
var len = idstr.length;

if (len != 36) {
return;
}
id = BigInt("0x" + idstr.substring(8, len - 8).toUpperCase()).toString(10);
console.log(id);
}
9 changes: 9 additions & 0 deletions tests/servers/rfid.sock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "RFID接收器",
"description": "用来接收并处理RIFD",
"version": "0.0.1",
"protocol": "tcp",
"host": "0.0.0.0",
"port": "3019",
"process": "flows.rfid.read"
}

0 comments on commit 6372faf

Please sign in to comment.