diff --git a/engine/load.go b/engine/load.go index bffcd4ff..74639faa 100644 --- a/engine/load.go +++ b/engine/load.go @@ -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 } diff --git a/sui/api/api.go b/sui/api/api.go new file mode 100644 index 00000000..c300e14d --- /dev/null +++ b/sui/api/api.go @@ -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(".yao", dsl, "sui.v1") + return err +} diff --git a/sui/api/process.go b/sui/api/process.go new file mode 100644 index 00000000..22a8e4b4 --- /dev/null +++ b/sui/api/process.go @@ -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..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 +} diff --git a/sui/api/process_test.go b/sui/api/process_test.go new file mode 100644 index 00000000..d25a781c --- /dev/null +++ b/sui/api/process_test.go @@ -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) + } +} diff --git a/sui/sui.go b/sui/api/sui.go similarity index 86% rename from sui/sui.go rename to sui/api/sui.go index aacffb56..5186b2a3 100644 --- a/sui/sui.go +++ b/sui/api/sui.go @@ -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 } diff --git a/sui/sui_test.go b/sui/api/sui_test.go similarity index 87% rename from sui/sui_test.go rename to sui/api/sui_test.go index 8e36b93b..d9b52239 100644 --- a/sui/sui_test.go +++ b/sui/api/sui_test.go @@ -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"]) diff --git a/sui/core/interfaces.go b/sui/core/interfaces.go index 6697f45d..abb58d60 100644 --- a/sui/core/interfaces.go +++ b/sui/core/interfaces.go @@ -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) diff --git a/sui/core/types.go b/sui/core/types.go index 7a8c984d..855f1b41 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -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:"-"`