From b82f6f60290f95db8b90fba212c9fd1ccda7c608 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 28 Feb 2024 21:21:56 +0800 Subject: [PATCH] Refactor build process and add data parameter to BuildOption --- sui/api/process.go | 10 ++++++++-- sui/core/sui.go | 18 ++++++++++++++++-- sui/core/types.go | 19 ++++++++++--------- sui/storages/local/build.go | 16 ++++++++-------- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/sui/api/process.go b/sui/api/process.go index a778214f..90211e41 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -914,12 +914,17 @@ func BuildAll(process *process.Process) interface{} { assetRoot = v } + data := map[string]interface{}{} + if v, ok := option["data"].(map[string]interface{}); ok { + data = v + } + tmpl, err := sui.GetTemplate(templateID) if err != nil { exception.New(err.Error(), 500).Throw() } - err = tmpl.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot}) + err = tmpl.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot, Data: data}) if err != nil { exception.New(err.Error(), 500).Throw() } @@ -959,7 +964,8 @@ func BuildPage(process *process.Process) interface{} { exception.New(err.Error(), 500).Throw() } - err = page.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot}) + data := process.ArgsMap(5, map[string]interface{}{}) + err = page.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot, Data: data}) if err != nil { exception.New(err.Error(), 500).Throw() } diff --git a/sui/core/sui.go b/sui/core/sui.go index 7c989c5a..b0e32c8c 100644 --- a/sui/core/sui.go +++ b/sui/core/sui.go @@ -73,18 +73,32 @@ func (sui *DSL) PublicRootWithSid(sid string) (string, error) { } // PublicRoot returns the public root path -func (sui *DSL) PublicRoot() (string, error) { +func (sui *DSL) PublicRoot(data map[string]interface{}) (string, error) { // Cache the public root if sui.publicRoot != "" { return sui.publicRoot, nil } + if data == nil { + data = map[string]interface{}{} + } + ss := session.Global().ID(sui.Sid) - data, err := ss.Dump() + sessionData, err := ss.Dump() if err != nil { return "", err } + // Merge the session data + if sessionData == nil { + sessionData = map[string]interface{}{} + } + + // Merge the data + for k, v := range sessionData { + data[k] = v + } + vars := map[string]interface{}{"$session": data} var root = sui.Public.Root dot := maps.Of(vars).Dot() diff --git a/sui/core/types.go b/sui/core/types.go index 390e7e6a..4744e1f6 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -136,15 +136,16 @@ type MediaSearchResult struct { // BuildOption is the struct for the option option type BuildOption struct { - SSR bool `json:"ssr"` - CDN bool `json:"cdn"` - UpdateAll bool `json:"update_all"` - AssetRoot string `json:"asset_root,omitempty"` - IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"` - IgnoreDocument bool `json:"ignore_document,omitempty"` - WithWrapper bool `json:"with_wrapper,omitempty"` - KeepPageTag bool `json:"keep_page_tag,omitempty"` - Namespace string `json:"namespace,omitempty"` + SSR bool `json:"ssr"` + CDN bool `json:"cdn"` + UpdateAll bool `json:"update_all"` + AssetRoot string `json:"asset_root,omitempty"` + IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"` + IgnoreDocument bool `json:"ignore_document,omitempty"` + WithWrapper bool `json:"with_wrapper,omitempty"` + KeepPageTag bool `json:"keep_page_tag,omitempty"` + Namespace string `json:"namespace,omitempty"` + Data map[string]interface{} `json:"data,omitempty"` } // Request is the struct for the request diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index 3a9ada42..bd6743a8 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -15,7 +15,7 @@ import ( func (tmpl *Template) Build(option *core.BuildOption) error { var err error - root, err := tmpl.local.DSL.PublicRoot() + root, err := tmpl.local.DSL.PublicRoot(option.Data) if err != nil { log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root) root = tmpl.local.DSL.Public.Root @@ -62,7 +62,7 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error { } //get target abs path - root, err := tmpl.local.DSL.PublicRoot() + root, err := tmpl.local.DSL.PublicRoot(option.Data) if err != nil { log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root) root = tmpl.local.DSL.Public.Root @@ -81,7 +81,7 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error { func (page *Page) Build(option *core.BuildOption) error { if option.AssetRoot == "" { - root, err := page.tmpl.local.DSL.PublicRoot() + root, err := page.tmpl.local.DSL.PublicRoot(option.Data) if err != nil { log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root) root = page.tmpl.local.DSL.Public.Root @@ -96,11 +96,11 @@ func (page *Page) Build(option *core.BuildOption) error { } // Save the html - return page.writeHTML([]byte(html)) + return page.writeHTML([]byte(html), option.Data) } -func (page *Page) publicFile() string { - root, err := page.tmpl.local.DSL.PublicRoot() +func (page *Page) publicFile(data map[string]interface{}) string { + root, err := page.tmpl.local.DSL.PublicRoot(data) if err != nil { log.Error("publicFile: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root) root = page.tmpl.local.DSL.Public.Root @@ -109,8 +109,8 @@ func (page *Page) publicFile() string { } // writeHTMLTo write the html to file -func (page *Page) writeHTML(html []byte) error { - htmlFile := fmt.Sprintf("%s.sui", page.publicFile()) +func (page *Page) writeHTML(html []byte, data map[string]interface{}) error { + htmlFile := fmt.Sprintf("%s.sui", page.publicFile(data)) htmlFileAbs := filepath.Join(application.App.Root(), htmlFile) dir := filepath.Dir(htmlFileAbs) if exist, _ := os.Stat(dir); exist == nil {