[add] SUI API
This commit is contained in:
parent
d005a64786
commit
ab7ace8a24
8 changed files with 127 additions and 16 deletions
|
|
@ -29,6 +29,7 @@ import (
|
|||
"github.com/yaoapp/yao/share"
|
||||
"github.com/yaoapp/yao/socket"
|
||||
"github.com/yaoapp/yao/store"
|
||||
sui "github.com/yaoapp/yao/sui/api"
|
||||
"github.com/yaoapp/yao/task"
|
||||
"github.com/yaoapp/yao/websocket"
|
||||
"github.com/yaoapp/yao/widget"
|
||||
|
|
@ -196,6 +197,12 @@ func Load(cfg config.Config) (err error) {
|
|||
printErr(cfg.Mode, "Widget", err)
|
||||
}
|
||||
|
||||
// Load SUI
|
||||
err = sui.Load(cfg)
|
||||
if err != nil {
|
||||
printErr(cfg.Mode, "SUI", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
27
sui/api/api.go
Normal file
27
sui/api/api.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package api
|
||||
|
||||
import "github.com/yaoapp/gou/api"
|
||||
|
||||
var dsl = []byte(`
|
||||
{
|
||||
"name": "SUI API",
|
||||
"description": "The API for SUI",
|
||||
"version": "1.0.0",
|
||||
"guard": "-",
|
||||
"group": "__yao/sui/v1",
|
||||
"paths": [
|
||||
{
|
||||
"path": "/template/:id",
|
||||
"method": "GET",
|
||||
"process": "sui.Template.Get",
|
||||
"in": ["$param.id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
}
|
||||
],
|
||||
}
|
||||
`)
|
||||
|
||||
func registerAPI() error {
|
||||
_, err := api.LoadSource("<sui.v1>.yao", dsl, "sui.v1")
|
||||
return err
|
||||
}
|
||||
33
sui/api/process.go
Normal file
33
sui/api/process.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/yaoapp/gou/process"
|
||||
"github.com/yaoapp/kun/exception"
|
||||
"github.com/yaoapp/yao/sui/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
process.RegisterGroup("sui", map[string]process.Handler{
|
||||
"template.get": TemplateGet,
|
||||
})
|
||||
}
|
||||
|
||||
// TemplateGet handle the get Template request
|
||||
// Process sui.<ID>.templates
|
||||
func TemplateGet(process *process.Process) interface{} {
|
||||
sui := get(process)
|
||||
templates, err := sui.GetTemplates()
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
return templates
|
||||
}
|
||||
|
||||
// get the sui
|
||||
func get(process *process.Process) core.SUI {
|
||||
sui, has := core.SUIs[process.ArgsString(0)]
|
||||
if !has {
|
||||
exception.New("the sui %s does not exist", 404, process.ID).Throw()
|
||||
}
|
||||
return sui
|
||||
}
|
||||
37
sui/api/process_test.go
Normal file
37
sui/api/process_test.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/gou/process"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/sui/core"
|
||||
)
|
||||
|
||||
func TestTemplates(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.template.get", "demo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.IsType(t, []core.ITemplate{}, res)
|
||||
assert.Equal(t, 3, len(res.([]core.ITemplate)))
|
||||
}
|
||||
|
||||
func load(t *testing.T) {
|
||||
prepare(t)
|
||||
err := Load(config.Conf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package sui
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -13,9 +13,6 @@ import (
|
|||
"github.com/yaoapp/yao/sui/storages/local"
|
||||
)
|
||||
|
||||
// SUIs the loaded SUI instances
|
||||
var SUIs = map[string]core.SUI{}
|
||||
|
||||
// New create a new sui
|
||||
func New(dsl *core.DSL) (core.SUI, error) {
|
||||
|
||||
|
|
@ -39,7 +36,7 @@ func New(dsl *core.DSL) (core.SUI, error) {
|
|||
// Load load the sui
|
||||
func Load(cfg config.Config) error {
|
||||
exts := []string{"*.sui.yao", "*.sui.jsonc", "*.sui.json"}
|
||||
return application.App.Walk("suis", func(root, file string, isdir bool) error {
|
||||
err := application.App.Walk("suis", func(root, file string, isdir bool) error {
|
||||
if isdir {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -52,6 +49,12 @@ func Load(cfg config.Config) error {
|
|||
}
|
||||
return nil
|
||||
}, exts...)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return registerAPI()
|
||||
}
|
||||
|
||||
func loadFile(file string, id string) (core.SUI, error) {
|
||||
|
|
@ -66,6 +69,6 @@ func loadFile(file string, id string) (core.SUI, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
SUIs[id] = sui
|
||||
return SUIs[id], nil
|
||||
core.SUIs[id] = sui
|
||||
return core.SUIs[id], nil
|
||||
}
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
package sui
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/sui/core"
|
||||
"github.com/yaoapp/yao/test"
|
||||
)
|
||||
|
||||
|
|
@ -22,7 +23,7 @@ func TestLoad(t *testing.T) {
|
|||
|
||||
func check(t *testing.T) {
|
||||
ids := map[string]bool{}
|
||||
for id := range SUIs {
|
||||
for id := range core.SUIs {
|
||||
ids[id] = true
|
||||
}
|
||||
assert.True(t, ids["azure"])
|
||||
|
|
@ -1,5 +1,15 @@
|
|||
package core
|
||||
|
||||
// SUIs the loaded SUI instances
|
||||
var SUIs = map[string]SUI{}
|
||||
|
||||
// SUI is the interface for the SUI
|
||||
type SUI interface {
|
||||
GetTemplates() ([]ITemplate, error)
|
||||
GetTemplate(name string) (ITemplate, error)
|
||||
UploadTemplate(src string, dst string) (ITemplate, error)
|
||||
}
|
||||
|
||||
// ITemplate is the interface for the ITemplate
|
||||
type ITemplate interface {
|
||||
Pages() ([]IPage, error)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,5 @@
|
|||
package core
|
||||
|
||||
// SUI is the interface for the SUI
|
||||
type SUI interface {
|
||||
GetTemplates() ([]ITemplate, error)
|
||||
GetTemplate(name string) (ITemplate, error)
|
||||
UploadTemplate(src string, dst string) (ITemplate, error)
|
||||
}
|
||||
|
||||
// DSL the struct for the DSL
|
||||
type DSL struct {
|
||||
ID string `json:"-"`
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue