From dabc8a0841a4f6b1370151c5527f45de26b9365e Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 27 Sep 2023 19:51:09 +0800 Subject: [PATCH 1/3] [change] sui template api route --- sui/api/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sui/api/api.go b/sui/api/api.go index c82bf64d..46916cf8 100644 --- a/sui/api/api.go +++ b/sui/api/api.go @@ -11,13 +11,13 @@ var dsl = []byte(` "group": "__yao/sui/v1", "paths": [ { - "path": "/:id/template/find/:template_id", + "path": "/:id/template/:template_id", "method": "GET", "process": "sui.Template.Find", "in": ["$param.id", "$param.template_id"], "out": { "status": 200, "type": "application/json" } },{ - "path": "/:id/template/get", + "path": "/:id/template", "method": "GET", "process": "sui.Template.Get", "in": ["$param.id"], From 2f42e13c04f6bd2569fae82912d1d9eb74da7e11 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 28 Sep 2023 10:11:01 +0800 Subject: [PATCH 2/3] [add] editor render API --- sui/api/api.go | 8 +++++++ sui/api/process.go | 42 +++++++++++++++++++++++++++++++++ sui/api/process_test.go | 19 +++++++++++++++ sui/core/data.go | 21 +++++++++++++++-- sui/core/interfaces.go | 2 +- sui/core/render.go | 8 ++++--- sui/core/types.go | 11 +++++---- sui/storages/local/page_test.go | 2 +- 8 files changed, 101 insertions(+), 12 deletions(-) diff --git a/sui/api/api.go b/sui/api/api.go index 46916cf8..aeca0adc 100644 --- a/sui/api/api.go +++ b/sui/api/api.go @@ -22,6 +22,14 @@ var dsl = []byte(` "process": "sui.Template.Get", "in": ["$param.id"], "out": { "status": 200, "type": "application/json" } + }, + + { + "path": "/:id/editor/render/:template_id/*route", + "method": "GET", + "process": "sui.Editor.Render", + "in": ["$param.id", "$param.template_id", "$param.route"], + "out": { "status": 200, "type": "application/json" } } ], } diff --git a/sui/api/process.go b/sui/api/process.go index cbf5b892..bfea05e6 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -10,6 +10,8 @@ func init() { process.RegisterGroup("sui", map[string]process.Handler{ "template.get": TemplateGet, "template.find": TemplateFind, + + "editor.render": EditorRender, }) } @@ -17,6 +19,7 @@ func init() { // Process sui..templates func TemplateGet(process *process.Process) interface{} { process.ValidateArgNums(1) + sui := get(process) templates, err := sui.GetTemplates() if err != nil { @@ -28,6 +31,7 @@ func TemplateGet(process *process.Process) interface{} { // TemplateFind handle the find Template request func TemplateFind(process *process.Process) interface{} { process.ValidateArgNums(2) + sui := get(process) template, err := sui.GetTemplate(process.ArgsString(1)) if err != nil { @@ -37,6 +41,44 @@ func TemplateFind(process *process.Process) interface{} { return template } +// EditorRender handle the render page request +func EditorRender(process *process.Process) interface{} { + process.ValidateArgNums(3) + + sui := get(process) + templateID := process.ArgsString(1) + route := process.ArgsString(2) + query := process.ArgsMap(3, map[string]interface{}{"method": "GET"}) + + if route == "" { + route = "/index" + } + + if route[0] != '/' { + route = "/" + route + } + + tmpl, err := sui.GetTemplate(templateID) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + page, err := tmpl.Page(route) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + // Request data + req := &core.Request{Method: query["method"].(string)} + + res, err := page.EditorRender(req) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + return res +} + // get the sui func get(process *process.Process) core.SUI { sui, has := core.SUIs[process.ArgsString(0)] diff --git a/sui/api/process_test.go b/sui/api/process_test.go index cbd2ae64..1e7dd396 100644 --- a/sui/api/process_test.go +++ b/sui/api/process_test.go @@ -48,6 +48,25 @@ func TestTemplateFind(t *testing.T) { assert.Equal(t, "tech-blue", res.(*local.Template).ID) } +func TestEditorRender(t *testing.T) { + load(t) + defer clean() + + // test demo + p, err := process.Of("sui.editor.render", "demo", "tech-blue", "/index") + if err != nil { + t.Fatal(err) + } + + res, err := p.Exec() + if err != nil { + t.Fatal(err) + } + + assert.IsType(t, &core.ResponseEditor{}, res) + assert.NotEmpty(t, res.(*core.ResponseEditor).HTML) +} + func load(t *testing.T) { prepare(t) err := Load(config.Conf) diff --git a/sui/core/data.go b/sui/core/data.go index 0fb82b0b..db1f09bf 100644 --- a/sui/core/data.go +++ b/sui/core/data.go @@ -1,8 +1,25 @@ package core +import ( + "strings" + + jsoniter "github.com/json-iterator/go" +) + // Data get the data -func (page *Page) Data(request *Request) (map[string]interface{}, error) { - return nil, nil +func (page *Page) Data(request *Request) (map[string]interface{}, map[string]interface{}, error) { + + setting := map[string]interface{}{ + "title": strings.ToUpper(page.Name), + } + + if page.Codes.DATA.Code != "" { + err := jsoniter.UnmarshalFromString(page.Codes.DATA.Code, &setting) + if err != nil { + return nil, nil, err + } + } + return nil, setting, nil } // RenderHTML render for the html diff --git a/sui/core/interfaces.go b/sui/core/interfaces.go index abb58d60..d0ffeaac 100644 --- a/sui/core/interfaces.go +++ b/sui/core/interfaces.go @@ -29,7 +29,7 @@ type ITemplate interface { // IPage is the interface for the page type IPage interface { Load() error - RenderEditor(request *Request) (*ResponseEditor, error) + EditorRender(request *Request) (*ResponseEditor, error) // Render() diff --git a/sui/core/render.go b/sui/core/render.go index 0ac4f857..127a6b92 100644 --- a/sui/core/render.go +++ b/sui/core/render.go @@ -9,8 +9,8 @@ import ( // Render render the page func (page *Page) Render() {} -// RenderEditor render for the editor -func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) { +// EditorRender render HTML for the editor +func (page *Page) EditorRender(request *Request) (*ResponseEditor, error) { html := page.Codes.HTML.Code res := &ResponseEditor{ @@ -19,6 +19,7 @@ func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) { Scripts: []string{}, Styles: []string{}, Warnings: []string{}, + Setting: map[string]interface{}{}, } // Get The scripts and styles @@ -50,11 +51,12 @@ func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) { res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".ts")) } - data, err := page.Data(request) + data, setting, err := page.Data(request) if err != nil { res.Warnings = append(res.Warnings, err.Error()) } + res.Setting = setting if data == nil { res.HTML = html return res, nil diff --git a/sui/core/types.go b/sui/core/types.go index 855f1b41..6effe126 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -70,11 +70,12 @@ type Request struct { // ResponseEditor is the struct for the response type ResponseEditor struct { - HTML string `json:"html,omitempty"` - CSS string `json:"css,omitempty"` - Scripts []string `json:"scripts,omitempty"` - Styles []string `json:"styles,omitempty"` - Warnings []string `json:"warnings,omitempty"` + HTML string `json:"html,omitempty"` + CSS string `json:"css,omitempty"` + Scripts []string `json:"scripts,omitempty"` + Styles []string `json:"styles,omitempty"` + Setting map[string]interface{} `json:"setting,omitempty"` + Warnings []string `json:"warnings,omitempty"` } // SourceCodes is the struct for the page codes diff --git a/sui/storages/local/page_test.go b/sui/storages/local/page_test.go index 2f5e3c70..2c13da4b 100644 --- a/sui/storages/local/page_test.go +++ b/sui/storages/local/page_test.go @@ -131,7 +131,7 @@ func TestPageRenderEditor(t *testing.T) { } r := &core.Request{Method: "GET"} - res, err := page.RenderEditor(r) + res, err := page.EditorRender(r) if err != nil { t.Fatalf("RenderEditor error: %v", err) } From 688e06e54894df0822051533fedcc7add0b1b8a4 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 28 Sep 2023 10:47:18 +0800 Subject: [PATCH 3/3] [add] editor source api --- sui/api/api.go | 6 +++ sui/api/process.go | 62 ++++++++++++++++++++++++++----- sui/api/process_test.go | 42 +++++++++++++++++++++ sui/core/{render.go => editor.go} | 43 ++++++++++++++++++--- sui/core/interfaces.go | 4 ++ sui/core/types.go | 6 +++ 6 files changed, 149 insertions(+), 14 deletions(-) rename sui/core/{render.go => editor.go} (74%) diff --git a/sui/api/api.go b/sui/api/api.go index aeca0adc..bd119dd8 100644 --- a/sui/api/api.go +++ b/sui/api/api.go @@ -30,6 +30,12 @@ var dsl = []byte(` "process": "sui.Editor.Render", "in": ["$param.id", "$param.template_id", "$param.route"], "out": { "status": 200, "type": "application/json" } + },{ + "path": "/:id/editor/:kind/source/:template_id/*route", + "method": "GET", + "process": "sui.Editor.Source", + "in": ["$param.id", "$param.template_id", "$param.route", "$param.kind"], + "out": { "status": 200, "type": "application/json" } } ], } diff --git a/sui/api/process.go b/sui/api/process.go index bfea05e6..430bf719 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -12,6 +12,7 @@ func init() { "template.find": TemplateFind, "editor.render": EditorRender, + "editor.source": EditorSource, }) } @@ -47,17 +48,9 @@ func EditorRender(process *process.Process) interface{} { sui := get(process) templateID := process.ArgsString(1) - route := process.ArgsString(2) + route := route(process, 2) query := process.ArgsMap(3, map[string]interface{}{"method": "GET"}) - if route == "" { - route = "/index" - } - - if route[0] != '/' { - route = "/" + route - } - tmpl, err := sui.GetTemplate(templateID) if err != nil { exception.New(err.Error(), 500).Throw() @@ -79,6 +72,45 @@ func EditorRender(process *process.Process) interface{} { return res } +// EditorSource handle the render page request +func EditorSource(process *process.Process) interface{} { + process.ValidateArgNums(3) + + sui := get(process) + templateID := process.ArgsString(1) + route := route(process, 2) + kind := process.ArgsString(3) + + tmpl, err := sui.GetTemplate(templateID) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + page, err := tmpl.Page(route) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + switch kind { + + case "page": + return page.EditorPageSource() + + case "style": + return page.EditorStyleSource() + + case "script": + return page.EditorScriptSource() + + case "data": + return page.EditorDataSource() + + default: + exception.New("the %s source does not exist", 404, kind).Throw() + return nil + } +} + // get the sui func get(process *process.Process) core.SUI { sui, has := core.SUIs[process.ArgsString(0)] @@ -87,3 +119,15 @@ func get(process *process.Process) core.SUI { } return sui } + +func route(process *process.Process, i int) string { + route := process.ArgsString(i) + if route == "" { + route = "/index" + } + + if route[0] != '/' { + route = "/" + route + } + return route +} diff --git a/sui/api/process_test.go b/sui/api/process_test.go index 1e7dd396..d2359885 100644 --- a/sui/api/process_test.go +++ b/sui/api/process_test.go @@ -67,6 +67,48 @@ func TestEditorRender(t *testing.T) { assert.NotEmpty(t, res.(*core.ResponseEditor).HTML) } +func TestEditorRenderWithQuery(t *testing.T) { + load(t) + defer clean() + + // test demo + p, err := process.Of("sui.editor.render", "demo", "tech-blue", "/index", map[string]interface{}{ + "method": "POST", + }) + if err != nil { + t.Fatal(err) + } + + res, err := p.Exec() + if err != nil { + t.Fatal(err) + } + + assert.IsType(t, &core.ResponseEditor{}, res) + assert.NotEmpty(t, res.(*core.ResponseEditor).HTML) +} + +func TestEditorPageSource(t *testing.T) { + load(t) + defer clean() + + sources := []string{"page", "script", "style", "data"} + for _, source := range sources { + p, err := process.Of("sui.editor.source", "demo", "tech-blue", "/index", source) + if err != nil { + t.Fatal(err) + } + + res, err := p.Exec() + if err != nil { + t.Fatal(err) + } + assert.IsType(t, core.ResponseSource{}, res) + assert.NotEmpty(t, res.(core.ResponseSource).Source) + assert.NotEmpty(t, res.(core.ResponseSource).Language) + } +} + func load(t *testing.T) { prepare(t) err := Load(config.Conf) diff --git a/sui/core/render.go b/sui/core/editor.go similarity index 74% rename from sui/core/render.go rename to sui/core/editor.go index 127a6b92..4e9df67a 100644 --- a/sui/core/render.go +++ b/sui/core/editor.go @@ -6,9 +6,6 @@ import ( "github.com/PuerkitoBio/goquery" ) -// Render render the page -func (page *Page) Render() {} - // EditorRender render HTML for the editor func (page *Page) EditorRender(request *Request) (*ResponseEditor, error) { @@ -73,8 +70,44 @@ func (page *Page) EditorRender(request *Request) (*ResponseEditor, error) { return res, nil } -// RenderPreview render for the preview -func (page *Page) RenderPreview() {} +// EditorPageSource get the editor page source code +func (page *Page) EditorPageSource() ResponseSource { + return ResponseSource{ + Source: page.Codes.HTML.Code, + Language: "html", + } +} + +// EditorScriptSource get the editor script source code +func (page *Page) EditorScriptSource() ResponseSource { + if page.Codes.TS.Code != "" { + return ResponseSource{ + Source: page.Codes.TS.Code, + Language: "typescript", + } + } + + return ResponseSource{ + Source: page.Codes.JS.Code, + Language: "javascript", + } +} + +// EditorStyleSource get the editor style source code +func (page *Page) EditorStyleSource() ResponseSource { + return ResponseSource{ + Source: page.Codes.CSS.Code, + Language: "css", + } +} + +// EditorDataSource get the editor data source code +func (page *Page) EditorDataSource() ResponseSource { + return ResponseSource{ + Source: page.Codes.DATA.Code, + Language: "json", + } +} // GlobalScripts get the global scripts func (page *Page) GlobalScripts() ([]string, error) { diff --git a/sui/core/interfaces.go b/sui/core/interfaces.go index d0ffeaac..a14d6249 100644 --- a/sui/core/interfaces.go +++ b/sui/core/interfaces.go @@ -30,6 +30,10 @@ type ITemplate interface { type IPage interface { Load() error EditorRender(request *Request) (*ResponseEditor, error) + EditorPageSource() ResponseSource + EditorScriptSource() ResponseSource + EditorStyleSource() ResponseSource + EditorDataSource() ResponseSource // Render() diff --git a/sui/core/types.go b/sui/core/types.go index 6effe126..a0b1f5b1 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -78,6 +78,12 @@ type ResponseEditor struct { Warnings []string `json:"warnings,omitempty"` } +// ResponseSource is the struct for the response +type ResponseSource struct { + Source string `json:"source,omitempty"` + Language string `json:"lang,omitempty"` +} + // SourceCodes is the struct for the page codes type SourceCodes struct { HTML Source `json:"-"`