Skip to content

Commit

Permalink
Merge pull request #477 from trheyi/main
Browse files Browse the repository at this point in the history
[add] page build api
  • Loading branch information
trheyi authored Oct 10, 2023
2 parents 4dfba1a + dd4e72a commit 0a5ea25
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 103 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
Expand All @@ -260,6 +261,8 @@ github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVH
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-plugin v1.4.4 h1:NVdrSdFRt3SkZtNckJ6tog7gbpRrcbOjQi/rgF7JYWQ=
github.com/hashicorp/go-plugin v1.4.4/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/go-plugin v1.5.1 h1:oGm7cWBaYIp3lJpx1RUEfLWophprE2EV/KUeqBYo+6k=
Expand Down
16 changes: 15 additions & 1 deletion sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,22 @@ var dsl = []byte(`
"path": "/:id/preview/:template_id/*route",
"method": "GET",
"process": "sui.Preview.Render",
"in": ["$param.id", "$param.template_id", "$param.path", "$header.Referer", "$query.r", "$query.t"],
"in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer", "$query.r", "$query.t"],
"out": {"status": 200, "type": "text/html; charset=utf-8"}
},
{
"path": "/:id/build/:template_id",
"method": "POST",
"process": "sui.Build.All",
"in": ["$param.id", "$param.template_id", ":payload"],
"out": {"status": 200}
},{
"path": "/:id/build/:template_id/*route",
"method": "POST",
"process": "sui.Build.Page",
"in": ["$param.id", "$param.template_id", "$param.route", ":payload"],
"out": {"status": 200}
}
],
}
Expand Down
74 changes: 74 additions & 0 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func init() {
"editor.sourceaftersavetemp": EditorSourceAfterSaveTemp,

"preview.render": PreviewRender,

"build.all": BuildAll,
"build.page": BuildPage,
})
}

Expand Down Expand Up @@ -571,6 +574,77 @@ func PreviewRender(process *process.Process) interface{} {
return html
}

// BuildAll handle the render page request
func BuildAll(process *process.Process) interface{} {

process.ValidateArgNums(3)
sui := get(process)
templateID := process.ArgsString(1)

option := process.ArgsMap(2, map[string]interface{}{})
ssr := true
if v, ok := option["ssr"].(bool); ok {
ssr = v
}

assetRoot := ""
if v, ok := option["asset_root"].(string); ok {
assetRoot = v
}

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

err = tmpl.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot})
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return nil
}

// BuildPage handle the render page request
func BuildPage(process *process.Process) interface{} {
process.ValidateArgNums(4)
sui := get(process)
templateID := process.ArgsString(1)
route := route(process, 2)
option := process.ArgsMap(3, map[string]interface{}{})
ssr := true
if v, ok := option["ssr"].(bool); ok {
ssr = v
}

assetRoot := ""
if v, ok := option["asset_root"].(string); ok {
assetRoot = v
}

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()
}

err = page.Load()
if err != nil {
exception.New(err.Error(), 500).Throw()
}

err = page.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot})
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return nil
}

// get the sui
func get(process *process.Process) core.SUI {
sui, has := core.SUIs[process.ArgsString(0)]
Expand Down
64 changes: 50 additions & 14 deletions sui/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,27 @@ func TestEditorRender(t *testing.T) {
assert.NotEmpty(t, res.(*core.ResponseEditorRender).Config)
}

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.SourceData{}, res)
assert.NotEmpty(t, res.(core.SourceData).Source)
assert.NotEmpty(t, res.(core.SourceData).Language)
}
}

func TestEditorRenderWithQuery(t *testing.T) {
load(t)
defer clean()
Expand Down Expand Up @@ -587,25 +608,40 @@ func TestPreviewRender(t *testing.T) {
assert.NotEmpty(t, res)
}

func TestEditorPageSource(t *testing.T) {
func TestBuildAll(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)
}
// test demo
p, err := process.Of("sui.build.all", "demo", "tech-blue", map[string]interface{}{"ssr": true})
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}
assert.IsType(t, core.SourceData{}, res)
assert.NotEmpty(t, res.(core.SourceData).Source)
assert.NotEmpty(t, res.(core.SourceData).Language)
res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.Nil(t, res)
}

func TestBuildPage(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.build.page", "demo", "tech-blue", "/index", map[string]interface{}{"ssr": true})
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.Nil(t, res)
}

func load(t *testing.T) {
Expand Down
96 changes: 96 additions & 0 deletions sui/core/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package core

import (
"fmt"
"strings"

"github.com/PuerkitoBio/goquery"
)

// Build is the struct for the public
func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error) {

warnings := []string{}
html, err := page.BuildHTML(option.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}

// Add Style & Script & Warning
doc, err := NewDocument([]byte(html))
if err != nil {
warnings = append(warnings, err.Error())
}

// Add Style
style, err := page.BuildStyle(option.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("head").AppendHtml(style)

// Add Script
script, err := page.BuildScript(option.AssetRoot)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("body").AppendHtml(script)
return doc, warnings, nil
}

// BuildHTML build the html
func (page *Page) BuildHTML(assetRoot string) (string, error) {

html := string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
}

code := strings.ReplaceAll(html, "@assets", assetRoot)
res, err := page.CompileHTML([]byte(code), false)
if err != nil {
return "", err
}

return string(res), nil
}

// BuildStyle build the style
func (page *Page) BuildStyle(assetRoot string) (string, error) {
if page.Codes.CSS.Code == "" {
return "", nil
}

code := strings.ReplaceAll(page.Codes.CSS.Code, "@assets", assetRoot)
res, err := page.CompileCSS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<style>\n%s\n</style>\n", res), nil
}

// BuildScript build the script
func (page *Page) BuildScript(assetRoot string) (string, error) {

if page.Codes.JS.Code == "" && page.Codes.TS.Code == "" {
return "", nil
}

if page.Codes.TS.Code != "" {
res, err := page.CompileTS([]byte(page.Codes.TS.Code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}

code := strings.ReplaceAll(page.Codes.JS.Code, "@assets", assetRoot)
res, err := page.CompileJS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}
59 changes: 0 additions & 59 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package core

import (
"fmt"
"regexp"
"strings"

"github.com/evanw/esbuild/pkg/api"
"github.com/yaoapp/gou/runtime/transform"
Expand Down Expand Up @@ -53,60 +51,3 @@ func (page *Page) CompileCSS(source []byte, minify bool) ([]byte, error) {
func (page *Page) CompileHTML(source []byte, minify bool) ([]byte, error) {
return source, nil
}

// BuildHTML build the html
func (page *Page) BuildHTML(assetRoot string) (string, error) {

html := string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
}

code := strings.ReplaceAll(html, "@assets", assetRoot)
res, err := page.CompileHTML([]byte(code), false)
if err != nil {
return "", err
}

return string(res), nil
}

// BuildStyle build the style
func (page *Page) BuildStyle(assetRoot string) (string, error) {
if page.Codes.CSS.Code == "" {
return "", nil
}

code := strings.ReplaceAll(page.Codes.CSS.Code, "@assets", assetRoot)
res, err := page.CompileCSS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<style>\n%s\n</style>\n", res), nil
}

// BuildScript build the script
func (page *Page) BuildScript(assetRoot string) (string, error) {

if page.Codes.JS.Code == "" && page.Codes.TS.Code == "" {
return "", nil
}

if page.Codes.TS.Code != "" {
res, err := page.CompileTS([]byte(page.Codes.TS.Code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}

code := strings.ReplaceAll(page.Codes.JS.Code, "@assets", assetRoot)
res, err := page.CompileCSS([]byte(code), false)
if err != nil {
return "", err
}

return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}
Loading

0 comments on commit 0a5ea25

Please sign in to comment.